Skip to content

Commit

Permalink
Fix incorrect code for nullable type arguments
Browse files Browse the repository at this point in the history
Closes #3300
  • Loading branch information
simolus3 committed Oct 23, 2024
1 parent 1a4aa06 commit c9e0734
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.22.0-dev

- Pass language version to dart formatter when generating code.
- Fix generated code missing question marks for nullable types in some instances.

## 2.21.0

Expand Down
4 changes: 4 additions & 0 deletions drift_dev/lib/src/analysis/results/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@ class _AddFromAst extends GeneralizingAstVisitor<void> {
if (node.typeArguments case final typeArgs?) {
visitTypeArgumentList(typeArgs);
}

if (node.question != null) {
_builder.addText('?');
}
}

@override
Expand Down
47 changes: 47 additions & 0 deletions drift_dev/test/backends/build/build_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1223,4 +1223,51 @@ CREATE TABLE b (foo INTEGER);
),
);
});

test('generates generic type converters correctly', () async {
// Regression test for https://github.com/simolus3/drift/issues/3300
final build = await emulateDriftBuild(
inputs: {
'a|lib/main.dart': '''
import 'dart:convert';
import 'package:drift/drift.dart';
part 'main.drift.dart';
class MapConverter<T> extends TypeConverter<Map<String, T>, String> {
@override
Map<String, T> fromSql(String fromDb) {
return Map<String, T>.from(jsonDecode(fromDb) ?? {});
}
@override
String toSql(Map<String, T> value) {
return jsonEncode(value);
}
}
class Users extends Table {
IntColumn get id => integer()();
TextColumn get extraData => text().map(MapConverter<Object?>())();
}
@DriftDatabase(tables: [Users])
class Database {}
''',
},
logger: loggerThat(neverEmits(anything)),
options: BuilderOptions({'generate_manager': false}),
);

checkOutputs(
{
'a|lib/main.drift.dart': decodedMatches(contains(r'''
static TypeConverter<Map<String, Object?>, String> $converterextraData =
MapConverter<Object?>();
'''))
},
build.dartOutputs,
build.writer,
);
});
}

0 comments on commit c9e0734

Please sign in to comment.