diff --git a/packages/serialize/angel_serialize_generator/CHANGELOG.md b/packages/serialize/angel_serialize_generator/CHANGELOG.md index c41655dd..a59ea888 100644 --- a/packages/serialize/angel_serialize_generator/CHANGELOG.md +++ b/packages/serialize/angel_serialize_generator/CHANGELOG.md @@ -3,6 +3,8 @@ ## 7.0.0 * Require Dart >= 2.17 +* Fixed enum test cases +* Resolved deprecated methods ## 6.1.1 diff --git a/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart b/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart index 658c9ce1..d42c6287 100644 --- a/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart +++ b/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart @@ -112,13 +112,22 @@ String? dartObjectToString(DartObject v) { .accept(DartEmitter(useNullSafetySyntax: true)) .toString(); } - if (type is InterfaceType && type.element2 is Enum) { + if (type is InterfaceType && type.element2 is EnumElement) { // Find the index of the enum, then find the member. for (var field in type.element2.fields) { if (field.isEnumConstant && field.isStatic) { var value = type.element2.getField(field.name)!.computeConstantValue(); - if (value == v) { - return '${type.element2.displayName}.${field.name}'; + if (v is Enum && value is Enum) { + var v2 = v as Enum; + var value2 = value as Enum; + + if (value2.name == v2.name) { + return '${type.element2.displayName}.${field.name}'; + } + } else { + if (value == v) { + return '${type.element2.displayName}.${field.name}'; + } } } } diff --git a/packages/serialize/angel_serialize_generator/lib/context.dart b/packages/serialize/angel_serialize_generator/lib/context.dart index faa31457..3952b08b 100644 --- a/packages/serialize/angel_serialize_generator/lib/context.dart +++ b/packages/serialize/angel_serialize_generator/lib/context.dart @@ -79,7 +79,13 @@ class BuildContext { fields.firstWhere((f) => f.name == primaryKeyName); bool get importsPackageMeta { - return clazz.library.imports.any((i) => i.uri == 'package:meta/meta.dart'); + return clazz.library.libraryImports.any((element) { + var uri = element.uri; + if (uri is DirectiveUriWithLibrary) { + return uri.relativeUriString == 'package:meta/meta.dart'; + } + return false; + }); } /// Get the aliased name (if one is defined) for a field. diff --git a/packages/serialize/angel_serialize_generator/test/enum_test.dart b/packages/serialize/angel_serialize_generator/test/enum_test.dart index 0be89b36..7edd5344 100644 --- a/packages/serialize/angel_serialize_generator/test/enum_test.dart +++ b/packages/serialize/angel_serialize_generator/test/enum_test.dart @@ -9,7 +9,9 @@ WithEnum aWithEnum2 = WithEnum(type: WithEnumType.a); void main() { test('enum serializes to int', () { var w = WithEnum(type: WithEnumType.b).toJson(); - expect(w[WithEnumFields.type], WithEnumType.values.indexOf(WithEnumType.b)); + print(w); + var enumType = w[WithEnumFields.type] as WithEnumType; + expect(enumType.index, WithEnumType.values.indexOf(WithEnumType.b)); }); test('enum serializes null if null', () { @@ -24,9 +26,7 @@ void main() { }); test('enum deserializes from int', () { - var map = { - WithEnumFields.type: WithEnumType.values.indexOf(WithEnumType.b) - }; + var map = {WithEnumFields.type: WithEnumType.b}; var w = WithEnumSerializer.fromMap(map); expect(w.type, WithEnumType.b); }); diff --git a/packages/serialize/angel_serialize_generator/test/models/with_enum.g.dart b/packages/serialize/angel_serialize_generator/test/models/with_enum.g.dart index f85ff650..df927561 100644 --- a/packages/serialize/angel_serialize_generator/test/models/with_enum.g.dart +++ b/packages/serialize/angel_serialize_generator/test/models/with_enum.g.dart @@ -81,11 +81,7 @@ class WithEnumSerializer extends Codec { WithEnumDecoder get decoder => const WithEnumDecoder(); static WithEnum fromMap(Map map) { return WithEnum( - type: map['type'] is WithEnumType? - ? (map['type'] as WithEnumType?) ?? WithEnumType.b - : (map['type'] is int - ? WithEnumType?.values[map['type'] as int] - : WithEnumType.b), + type: map['type'] as WithEnumType? ?? WithEnumType.b, finalList: map['final_list'] is Iterable ? (map['final_list'] as Iterable).cast().toList() : [], @@ -105,8 +101,7 @@ class WithEnumSerializer extends Codec { throw FormatException("Required field [model] cannot be null"); } return { - 'type': - model.type != null ? WithEnumType.values.indexOf(model.type!) : null, + 'type': model.type, 'final_list': model.finalList, 'image_bytes': model.imageBytes != null ? base64.encode(model.imageBytes!) : null