diff --git a/angel_serialize_generator/CHANGELOG.md b/angel_serialize_generator/CHANGELOG.md index 16b6f04a..910e94b1 100644 --- a/angel_serialize_generator/CHANGELOG.md +++ b/angel_serialize_generator/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.8+1 +* Fix generation of `const` constructors with iterables. + # 2.0.8 * Now supports de/serialization of `enum` types. * Generate `const` constructors when possible. diff --git a/angel_serialize_generator/lib/model.dart b/angel_serialize_generator/lib/model.dart index 1a767c1b..ad7cd81c 100644 --- a/angel_serialize_generator/lib/model.dart +++ b/angel_serialize_generator/lib/model.dart @@ -86,7 +86,7 @@ class JsonModelGenerator extends GeneratorForAnnotation { } for (var field in ctx.fields) { - if (isListOrMapType(field.type)) { + if (!shouldBeConstant(ctx) && isListOrMapType(field.type)) { String typeName = const TypeChecker.fromRuntime(List) .isAssignableFromType(field.type) ? 'List' @@ -98,15 +98,10 @@ class JsonModelGenerator extends GeneratorForAnnotation { } } - if (ctx.constructorParameters.isNotEmpty) { - constructor.initializers.add( - new Code('super(${ctx.constructorParameters.map((p) => p.name).join( - ',')})')); - } - for (var field in ctx.fields) { constructor.optionalParameters.add(new Parameter((b) { b + ..toThis = shouldBeConstant(ctx) ..name = field.name ..named = true; @@ -121,6 +116,14 @@ class JsonModelGenerator extends GeneratorForAnnotation { } })); } + + if (ctx.constructorParameters.isNotEmpty) { + if (!shouldBeConstant(ctx) || + ctx.clazz.unnamedConstructor?.isConst == true) + constructor.initializers.add(new Code( + 'super(${ctx.constructorParameters.map((p) => p.name).join( + ',')})')); + } })); } diff --git a/angel_serialize_generator/pubspec.yaml b/angel_serialize_generator/pubspec.yaml index df87d144..ee7b98d1 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 +version: 2.0.8+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/models/with_enum.dart b/angel_serialize_generator/test/models/with_enum.dart index 91d4811a..f8c08dc5 100644 --- a/angel_serialize_generator/test/models/with_enum.dart +++ b/angel_serialize_generator/test/models/with_enum.dart @@ -1,11 +1,13 @@ -import 'package:angel_model/angel_model.dart'; import 'package:angel_serialize/angel_serialize.dart'; +import 'package:collection/collection.dart'; part 'with_enum.g.dart'; part 'with_enum.serializer.g.dart'; @Serializable(autoIdAndDateFields: false) abstract class _WithEnum { WithEnumType get type; + + List get finalList; } enum WithEnumType { a, b, c } diff --git a/angel_serialize_generator/test/models/with_enum.g.dart b/angel_serialize_generator/test/models/with_enum.g.dart index f5f08a08..aeef25e6 100644 --- a/angel_serialize_generator/test/models/with_enum.g.dart +++ b/angel_serialize_generator/test/models/with_enum.g.dart @@ -7,17 +7,24 @@ part of 'with_enum.dart'; // ************************************************************************** class WithEnum implements _WithEnum { - const WithEnum({this.type}); + const WithEnum({this.type, List this.finalList}); @override final WithEnumType type; - WithEnum copyWith({WithEnumType type}) { - return new WithEnum(type: type ?? this.type); + @override + final List finalList; + + WithEnum copyWith({WithEnumType type, List finalList}) { + return new WithEnum( + type: type ?? this.type, finalList: finalList ?? this.finalList); } bool operator ==(other) { - return other is _WithEnum && other.type == type; + return other is _WithEnum && + other.type == type && + const ListEquality(const DefaultEquality()) + .equals(other.finalList, finalList); } Map toJson() { 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 b7c19328..ff247489 100644 --- a/angel_serialize_generator/test/models/with_enum.serializer.g.dart +++ b/angel_serialize_generator/test/models/with_enum.serializer.g.dart @@ -11,17 +11,21 @@ abstract class WithEnumSerializer { return new WithEnum( type: map['type'] is WithEnumType ? map['type'] - : (map['type'] is int ? WithEnumType.values[map['type']] : null)); + : (map['type'] is int ? WithEnumType.values[map['type']] : null), + finalList: map['final_list'] as List); } static Map toMap(WithEnum model) { return { 'type': - model.type == null ? null : WithEnumType.values.indexOf(model.type) + model.type == null ? null : WithEnumType.values.indexOf(model.type), + 'final_list': model.finalList }; } } abstract class WithEnumFields { static const String type = 'type'; + + static const String finalList = 'final_list'; }