From c755d53b7b68bfa9e5662db520a6abd8c09466ab Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 17 Dec 2024 20:42:42 -0300 Subject: [PATCH] chore: add test for alias type --- json_annotation/lib/src/json_value.dart | 2 +- json_serializable/lib/src/enum_utils.dart | 2 +- json_serializable/test/json_serializable_test.dart | 1 + json_serializable/test/src/_json_enum_test_input.dart | 10 ++++++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/json_annotation/lib/src/json_value.dart b/json_annotation/lib/src/json_value.dart index 9ada231e..243d85a0 100644 --- a/json_annotation/lib/src/json_value.dart +++ b/json_annotation/lib/src/json_value.dart @@ -12,7 +12,7 @@ class JsonValue { /// Optional values that can be used when deserializing. /// /// The elements of [aliases] must be either [String] or [int]. - final Set aliases; + final Set aliases; const JsonValue(this.value, {this.aliases = const {}}); } diff --git a/json_serializable/lib/src/enum_utils.dart b/json_serializable/lib/src/enum_utils.dart index 050f0990..44f68632 100644 --- a/json_serializable/lib/src/enum_utils.dart +++ b/json_serializable/lib/src/enum_utils.dart @@ -216,7 +216,7 @@ List _generateAliases({ final targetTypeCode = typeToCode(targetType); throw InvalidGenerationSourceError( 'The `JsonValue` annotation on `$targetTypeCode.${field.name}` aliases ' - 'should all be of type String, or int.', + 'should all be of type String or int.', element: field, ); } diff --git a/json_serializable/test/json_serializable_test.dart b/json_serializable/test/json_serializable_test.dart index b6ff3678..b6ef4a57 100644 --- a/json_serializable/test/json_serializable_test.dart +++ b/json_serializable/test/json_serializable_test.dart @@ -32,6 +32,7 @@ Future main() async { jsonEnumTestReader, const JsonEnumGenerator(), expectedAnnotatedTests: { + 'EnumAliasNotSupportType', 'EnumValueIssue1147', 'EnumValueNotAField', 'EnumValueNotSupportType', diff --git a/json_serializable/test/src/_json_enum_test_input.dart b/json_serializable/test/src/_json_enum_test_input.dart index fed99e48..91a797c5 100644 --- a/json_serializable/test/src/_json_enum_test_input.dart +++ b/json_serializable/test/src/_json_enum_test_input.dart @@ -5,6 +5,16 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:source_gen_test/annotations.dart'; +@ShouldThrow( + 'The `JsonValue` annotation on `EnumAliasNotSupportType.one` aliases should ' + 'all be of type String or int.', +) +@JsonEnum(alwaysCreate: true) +enum EnumAliasNotSupportType { + @JsonValue('one', aliases: {#one}) + one, +} + @ShouldGenerate(r''' // ignore: unused_element const _$EnumValueIssue1147EnumMap = {