diff --git a/angel_serialize_generator/CHANGELOG.md b/angel_serialize_generator/CHANGELOG.md new file mode 100644 index 00000000..1222e07f --- /dev/null +++ b/angel_serialize_generator/CHANGELOG.md @@ -0,0 +1,3 @@ +# 2.0.1 +* Ensured that `List` is only transformed if +it generically references a `Model`. \ No newline at end of file diff --git a/angel_serialize_generator/lib/angel_serialize_generator.dart b/angel_serialize_generator/lib/angel_serialize_generator.dart index f07634a1..66b99994 100644 --- a/angel_serialize_generator/lib/angel_serialize_generator.dart +++ b/angel_serialize_generator/lib/angel_serialize_generator.dart @@ -53,6 +53,14 @@ bool isModelClass(DartType t) { } } +/// Determines if a [DartType] is a `List` with the first type argument being a `Model`. +bool isListModelType(InterfaceType t) { + return t.name == 'List' && + t.typeArguments.length == 1 && + isModelClass(t.typeArguments[0]); +} + + /// Determines if a [DartType] is a `Map` with the second type argument being a `Model`. bool isMapToModelType(InterfaceType t) { return t.name == 'Map' && diff --git a/angel_serialize_generator/lib/serialize.dart b/angel_serialize_generator/lib/serialize.dart index 4e3a270c..91026185 100644 --- a/angel_serialize_generator/lib/serialize.dart +++ b/angel_serialize_generator/lib/serialize.dart @@ -89,7 +89,7 @@ class SerializerGenerator extends GeneratorForAnnotation { } else if (field.type is InterfaceType) { var t = field.type as InterfaceType; - if (t.name == 'List' && t.typeArguments.length == 1) { + if (isListModelType(t)) { var rc = new ReCase(t.typeArguments[0].name); serializedRepresentation = 'model.${field.name}?.map(${rc .pascalCase}Serializer.toMap)?.toList()'; @@ -157,7 +157,7 @@ class SerializerGenerator extends GeneratorForAnnotation { } else if (field.type is InterfaceType) { var t = field.type as InterfaceType; - if (t.name == 'List' && t.typeArguments.length == 1) { + if (isListModelType(t)) { var rc = new ReCase(t.typeArguments[0].name); deserializedRepresentation = "map['$alias'] is Iterable" " ? map['$alias'].map(${rc diff --git a/angel_serialize_generator/pubspec.yaml b/angel_serialize_generator/pubspec.yaml index d292cf35..61d26f16 100644 --- a/angel_serialize_generator/pubspec.yaml +++ b/angel_serialize_generator/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_serialize_generator -version: 2.0.0 +version: 2.0.1 description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling. author: Tobe O homepage: https://github.com/angel-dart/serialize diff --git a/angel_serialize_generator/test/book_test.dart b/angel_serialize_generator/test/book_test.dart index 5a15d081..a9f03091 100644 --- a/angel_serialize_generator/test/book_test.dart +++ b/angel_serialize_generator/test/book_test.dart @@ -11,6 +11,7 @@ main() { title: 'Harry Potter and the Deathly Hallows', description: 'The 7th book.', pageCount: 759, + notModels: [1.0, 3.0], updatedAt: new DateTime.now()); var serializedDeathlyHallows = deathlyHallows.toJson(); print('Deathly Hallows: $serializedDeathlyHallows'); @@ -46,6 +47,10 @@ main() { expect(serializedDeathlyHallows.keys, isNot(contains('pageCount'))); }); + test('standard list', () { + expect(serializedDeathlyHallows['not_models'], deathlyHallows.notModels); + }); + test('heeds @exclude', () { expect(serializedJkRowling.keys, isNot(contains('secret'))); }); @@ -81,6 +86,7 @@ main() { expect(book.author, deathlyHallows.author); expect(book.description, deathlyHallows.description); expect(book.pageCount, deathlyHallows.pageCount); + expect(book.notModels, deathlyHallows.notModels); expect(book.createdAt, isNull); expect(book.updatedAt, deathlyHallows.updatedAt); }); diff --git a/angel_serialize_generator/test/models/book.dart b/angel_serialize_generator/test/models/book.dart index bc938f72..92dc0319 100644 --- a/angel_serialize_generator/test/models/book.dart +++ b/angel_serialize_generator/test/models/book.dart @@ -9,4 +9,5 @@ part 'book.serializer.g.dart'; abstract class _Book extends Model { String author, title, description; int pageCount; + List notModels; } \ No newline at end of file diff --git a/angel_serialize_generator/test/models/book.g.dart b/angel_serialize_generator/test/models/book.g.dart index d6812325..7140019e 100644 --- a/angel_serialize_generator/test/models/book.g.dart +++ b/angel_serialize_generator/test/models/book.g.dart @@ -13,6 +13,7 @@ class Book extends _Book { this.title, this.description, this.pageCount, + this.notModels, this.createdAt, this.updatedAt}); @@ -31,6 +32,9 @@ class Book extends _Book { @override final int pageCount; + @override + final List notModels; + @override final DateTime createdAt; @@ -43,6 +47,7 @@ class Book extends _Book { String title, String description, int pageCount, + List notModels, DateTime createdAt, DateTime updatedAt}) { return new Book( @@ -51,6 +56,7 @@ class Book extends _Book { title: title ?? this.title, description: description ?? this.description, pageCount: pageCount ?? this.pageCount, + notModels: notModels ?? this.notModels, createdAt: createdAt ?? this.createdAt, updatedAt: updatedAt ?? this.updatedAt); } diff --git a/angel_serialize_generator/test/models/book.serializer.g.dart b/angel_serialize_generator/test/models/book.serializer.g.dart index 7ab5ab17..fbcadf2c 100644 --- a/angel_serialize_generator/test/models/book.serializer.g.dart +++ b/angel_serialize_generator/test/models/book.serializer.g.dart @@ -13,6 +13,7 @@ abstract class BookSerializer { String title, String description, int pageCount, + List notModels, DateTime createdAt, DateTime updatedAt}) { return new Book( @@ -21,6 +22,7 @@ abstract class BookSerializer { title: map['title'], description: map['description'], pageCount: map['page_count'], + notModels: map['not_models'], createdAt: map['created_at'] != null ? DateTime.parse(map['created_at']) : null, @@ -36,6 +38,7 @@ abstract class BookSerializer { 'title': model.title, 'description': model.description, 'page_count': model.pageCount, + 'not_models': model.notModels, 'created_at': model.createdAt?.toIso8601String(), 'updated_at': model.updatedAt?.toIso8601String() };