diff --git a/angel_serialize_generator/CHANGELOG.md b/angel_serialize_generator/CHANGELOG.md index e58ce976..b26a2519 100644 --- a/angel_serialize_generator/CHANGELOG.md +++ b/angel_serialize_generator/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.0.8+3 +* Don't fail on `null` in `toMap`. +* Support self-referencing via `toJson()`. + # 2.0.8+2 * Better discern when custom methods disqualify classes from `const` protection. diff --git a/angel_serialize_generator/lib/serialize.dart b/angel_serialize_generator/lib/serialize.dart index b544acd7..b8e1aa78 100644 --- a/angel_serialize_generator/lib/serialize.dart +++ b/angel_serialize_generator/lib/serialize.dart @@ -89,6 +89,14 @@ class SerializerGenerator extends GeneratorForAnnotation { String serializedRepresentation = 'model.${field.name}'; + String serializerToMap(ReCase rc, String value) { + if (rc.pascalCase == ctx.modelClassName) { + return '($value)?.toJson()'; + } + + return '${rc.pascalCase}Serializer.toMap($value)'; + } + // Serialize dates if (dateTimeTypeChecker.isAssignableFromType(field.type)) serializedRepresentation = 'model.${field.name}?.toIso8601String()'; @@ -97,7 +105,7 @@ class SerializerGenerator extends GeneratorForAnnotation { else if (isModelClass(field.type)) { var rc = new ReCase(field.type.name); serializedRepresentation = - '${rc.pascalCase}Serializer.toMap(model.${field.name})'; + '${serializerToMap(rc, 'model.${field.name}')}'; } else if (field.type is InterfaceType) { var t = field.type as InterfaceType; @@ -109,8 +117,8 @@ class SerializerGenerator extends GeneratorForAnnotation { var rc = new ReCase(t.typeArguments[1].name); serializedRepresentation = '''model.${field.name}.keys?.fold({}, (map, key) { - return map..[key] = ${rc.pascalCase}Serializer.toMap(model.${field - .name}[key]); + return map..[key] = ${serializerToMap( + rc, 'model.${field.name}[key]')}; })'''; } else if (t.element.isEnum) { serializedRepresentation = ''' @@ -125,7 +133,10 @@ class SerializerGenerator extends GeneratorForAnnotation { } buf.write('};'); - method.body = new Code(buf.toString()); + method.body = new Block.of([ + new Code('if (model == null) { return null; }'), + new Code(buf.toString()), + ]); })); } diff --git a/angel_serialize_generator/pubspec.yaml b/angel_serialize_generator/pubspec.yaml index f820122b..12edf8e6 100644 --- a/angel_serialize_generator/pubspec.yaml +++ b/angel_serialize_generator/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_serialize_generator -version: 2.0.8+2 +version: 2.0.8+3 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/models/author.serializer.g.dart b/angel_serialize_generator/test/models/author.serializer.g.dart index 5f0c5a52..7ddaf4f6 100644 --- a/angel_serialize_generator/test/models/author.serializer.g.dart +++ b/angel_serialize_generator/test/models/author.serializer.g.dart @@ -42,6 +42,9 @@ abstract class AuthorSerializer { } static Map toMap(Author model) { + if (model == null) { + return null; + } if (model.name == null) { throw new FormatException("Missing required field 'name' on Author."); } @@ -107,6 +110,9 @@ abstract class LibrarySerializer { } static Map toMap(Library model) { + if (model == null) { + return null; + } return { 'id': model.id, 'collection': model.collection.keys?.fold({}, (map, key) { @@ -152,6 +158,9 @@ abstract class BookmarkSerializer { } static Map toMap(Bookmark model) { + if (model == null) { + return null; + } if (model.page == null) { throw new FormatException("Missing required field 'page' on Bookmark."); } diff --git a/angel_serialize_generator/test/models/book.serializer.g.dart b/angel_serialize_generator/test/models/book.serializer.g.dart index 44b49b73..01505d31 100644 --- a/angel_serialize_generator/test/models/book.serializer.g.dart +++ b/angel_serialize_generator/test/models/book.serializer.g.dart @@ -29,6 +29,9 @@ abstract class BookSerializer { } static Map toMap(Book model) { + if (model == null) { + return null; + } return { 'id': model.id, 'author': model.author, diff --git a/angel_serialize_generator/test/models/with_enum.serializer.g.dart b/angel_serialize_generator/test/models/with_enum.serializer.g.dart index ff247489..7e2d7879 100644 --- a/angel_serialize_generator/test/models/with_enum.serializer.g.dart +++ b/angel_serialize_generator/test/models/with_enum.serializer.g.dart @@ -16,6 +16,9 @@ abstract class WithEnumSerializer { } static Map toMap(WithEnum model) { + if (model == null) { + return null; + } return { 'type': model.type == null ? null : WithEnumType.values.indexOf(model.type),