From 3317ecbd5ebbd8e1b9f7f4e5a0d7466f0a2cc7a3 Mon Sep 17 00:00:00 2001 From: Guy Kogus Date: Thu, 18 Mar 2021 12:36:14 +0100 Subject: [PATCH 1/6] Add allowUnsupportedEnumValue to JsonKey --- json_annotation/lib/src/json_key.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/json_annotation/lib/src/json_key.dart b/json_annotation/lib/src/json_key.dart index 718a87187..2431cef95 100644 --- a/json_annotation/lib/src/json_key.dart +++ b/json_annotation/lib/src/json_key.dart @@ -103,6 +103,9 @@ class JsonKey { /// Valid only on enum fields with a compatible enum value. final Object unknownEnumValue; + /// If `true`, allow the unknown enum value to be `null`. + final bool allowUnsupportedEnumValue; + /// Creates a new [JsonKey] instance. /// /// Only required when the default behavior is not desired. @@ -117,5 +120,6 @@ class JsonKey { this.required, this.toJson, this.unknownEnumValue, + this.allowUnsupportedEnumValue, }); } From 8c20710f83f145bf252bc892ddaa9e2caf97f3d0 Mon Sep 17 00:00:00 2001 From: Guy Kogus Date: Thu, 18 Mar 2021 14:59:11 +0100 Subject: [PATCH 2/6] Allow unhandled enum values to be nullable --- json_annotation/lib/src/json_key.dart | 4 ---- .../lib/src/type_helpers/enum_helper.dart | 11 +++++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/json_annotation/lib/src/json_key.dart b/json_annotation/lib/src/json_key.dart index 2431cef95..718a87187 100644 --- a/json_annotation/lib/src/json_key.dart +++ b/json_annotation/lib/src/json_key.dart @@ -103,9 +103,6 @@ class JsonKey { /// Valid only on enum fields with a compatible enum value. final Object unknownEnumValue; - /// If `true`, allow the unknown enum value to be `null`. - final bool allowUnsupportedEnumValue; - /// Creates a new [JsonKey] instance. /// /// Only required when the default behavior is not desired. @@ -120,6 +117,5 @@ class JsonKey { this.required, this.toJson, this.unknownEnumValue, - this.allowUnsupportedEnumValue, }); } diff --git a/json_serializable/lib/src/type_helpers/enum_helper.dart b/json_serializable/lib/src/type_helpers/enum_helper.dart index bf1128fcd..0651cc670 100644 --- a/json_serializable/lib/src/type_helpers/enum_helper.dart +++ b/json_serializable/lib/src/type_helpers/enum_helper.dart @@ -96,14 +96,14 @@ T _$enumDecode( } final value = enumValues.entries - .singleWhere((e) => e.value == source, orElse: () => null) + .singleWhere((e) => e.value == source, orElse: () => unknownValue) ?.key; - if (value == null && unknownValue == null) { + if (value == null) { throw ArgumentError('`$source` is not one of the supported values: ' '${enumValues.values.join(', ')}'); } - return value ?? unknownValue; + return value; } '''; @@ -116,5 +116,8 @@ T _$enumDecodeNullable( if (source == null) { return null; } - return _$enumDecode(enumValues, source, unknownValue: unknownValue); + + return enumValues.entries + .singleWhere((e) => e.value == source, orElse: () => null) + ?.key; }'''; From a25e741dd1f86bb35273014788b4b27edff6fa73 Mon Sep 17 00:00:00 2001 From: Guy Kogus Date: Thu, 18 Mar 2021 15:14:52 +0100 Subject: [PATCH 3/6] Return unsupported enum values as null --- .../lib/src/type_helpers/enum_helper.dart | 7 ++++--- .../test/integration/integration_test.dart | 14 +++++++++++++ .../test/integration/json_test_example.dart | 20 +++++++++++++++++++ .../test/integration/json_test_example.g.dart | 20 ++++++++++++++++++- .../json_test_example.g_non_nullable.dart | 20 +++++++++++++++++++ .../json_test_example.g_non_nullable.g.dart | 14 +++++++++++++ 6 files changed, 91 insertions(+), 4 deletions(-) diff --git a/json_serializable/lib/src/type_helpers/enum_helper.dart b/json_serializable/lib/src/type_helpers/enum_helper.dart index 0651cc670..4cf65c23b 100644 --- a/json_serializable/lib/src/type_helpers/enum_helper.dart +++ b/json_serializable/lib/src/type_helpers/enum_helper.dart @@ -96,8 +96,9 @@ T _$enumDecode( } final value = enumValues.entries - .singleWhere((e) => e.value == source, orElse: () => unknownValue) - ?.key; + .singleWhere((e) => e.value == source, orElse: () => null) + ?.key ?? + unknownValue; if (value == null) { throw ArgumentError('`$source` is not one of the supported values: ' @@ -119,5 +120,5 @@ T _$enumDecodeNullable( return enumValues.entries .singleWhere((e) => e.value == source, orElse: () => null) - ?.key; + ?.key ?? unknownValue; }'''; diff --git a/json_serializable/test/integration/integration_test.dart b/json_serializable/test/integration/integration_test.dart index fa51cfbe2..5dbff741b 100644 --- a/json_serializable/test/integration/integration_test.dart +++ b/json_serializable/test/integration/integration_test.dart @@ -280,4 +280,18 @@ void main() { expect(instance.enumList, [Category.notDiscoveredYet]); expect(instance.enumSet, [Category.notDiscoveredYet]); }); + + test('NullableUnknownEnumValue', () { + final instance = NullableUnknownEnumValue.fromJson({ + 'enumValue': 'nope', + 'enumIterable': ['nope'], + 'enumList': ['nope'], + 'enumSet': ['nope'], + }); + + expect(instance.enumValue, null); + expect(instance.enumIterable, [null]); + expect(instance.enumList, [null]); + expect(instance.enumSet, [null]); + }); } diff --git a/json_serializable/test/integration/json_test_example.dart b/json_serializable/test/integration/json_test_example.dart index f23831961..40b867c62 100644 --- a/json_serializable/test/integration/json_test_example.dart +++ b/json_serializable/test/integration/json_test_example.dart @@ -194,3 +194,23 @@ class UnknownEnumValue { factory UnknownEnumValue.fromJson(Map json) => _$UnknownEnumValueFromJson(json); } + +@JsonSerializable(createToJson: false) +class NullableUnknownEnumValue { + @JsonKey() + Category enumValue; + + @JsonKey() + Iterable enumIterable; + + @JsonKey() + List enumList; + + @JsonKey() + Set enumSet; + + NullableUnknownEnumValue(); + + factory NullableUnknownEnumValue.fromJson(Map json) => + _$NullableUnknownEnumValueFromJson(json); +} diff --git a/json_serializable/test/integration/json_test_example.g.dart b/json_serializable/test/integration/json_test_example.g.dart index 4ec605cc3..6846f70b3 100644 --- a/json_serializable/test/integration/json_test_example.g.dart +++ b/json_serializable/test/integration/json_test_example.g.dart @@ -76,7 +76,11 @@ T _$enumDecodeNullable( if (source == null) { return null; } - return _$enumDecode(enumValues, source, unknownValue: unknownValue); + + return enumValues.entries + .singleWhere((e) => e.value == source, orElse: () => null) + ?.key ?? + unknownValue; } const _$CategoryEnumMap = { @@ -234,3 +238,17 @@ UnknownEnumValue _$UnknownEnumValueFromJson(Map json) { unknownValue: Category.notDiscoveredYet)) ?.toSet(); } + +NullableUnknownEnumValue _$NullableUnknownEnumValueFromJson( + Map json) { + return NullableUnknownEnumValue() + ..enumValue = _$enumDecodeNullable(_$CategoryEnumMap, json['enumValue']) + ..enumIterable = (json['enumIterable'] as List) + .map((e) => _$enumDecodeNullable(_$CategoryEnumMap, e)) + ..enumList = (json['enumList'] as List) + .map((e) => _$enumDecodeNullable(_$CategoryEnumMap, e)) + .toList() + ..enumSet = (json['enumSet'] as List) + .map((e) => _$enumDecodeNullable(_$CategoryEnumMap, e)) + .toSet(); +} diff --git a/json_serializable/test/integration/json_test_example.g_non_nullable.dart b/json_serializable/test/integration/json_test_example.g_non_nullable.dart index cb9928180..a29e2b904 100644 --- a/json_serializable/test/integration/json_test_example.g_non_nullable.dart +++ b/json_serializable/test/integration/json_test_example.g_non_nullable.dart @@ -204,3 +204,23 @@ class UnknownEnumValue { factory UnknownEnumValue.fromJson(Map json) => _$UnknownEnumValueFromJson(json); } + +@JsonSerializable(createToJson: false) +class NullableUnknownEnumValue { + @JsonKey() + Category enumValue; + + @JsonKey() + Iterable enumIterable; + + @JsonKey() + List enumList; + + @JsonKey() + Set enumSet; + + NullableUnknownEnumValue(); + + factory NullableUnknownEnumValue.fromJson(Map json) => + _$NullableUnknownEnumValueFromJson(json); +} diff --git a/json_serializable/test/integration/json_test_example.g_non_nullable.g.dart b/json_serializable/test/integration/json_test_example.g_non_nullable.g.dart index 6c0e324d6..90f2341ce 100644 --- a/json_serializable/test/integration/json_test_example.g_non_nullable.g.dart +++ b/json_serializable/test/integration/json_test_example.g_non_nullable.g.dart @@ -201,3 +201,17 @@ UnknownEnumValue _$UnknownEnumValueFromJson(Map json) { unknownValue: Category.notDiscoveredYet)) .toSet(); } + +NullableUnknownEnumValue _$NullableUnknownEnumValueFromJson( + Map json) { + return NullableUnknownEnumValue() + ..enumValue = _$enumDecodeNullable(_$CategoryEnumMap, json['enumValue']) + ..enumIterable = (json['enumIterable'] as List) + .map((e) => _$enumDecodeNullable(_$CategoryEnumMap, e)) + ..enumList = (json['enumList'] as List) + .map((e) => _$enumDecodeNullable(_$CategoryEnumMap, e)) + .toList() + ..enumSet = (json['enumSet'] as List) + .map((e) => _$enumDecodeNullable(_$CategoryEnumMap, e)) + .toSet(); +} From d0f98923c76112881c139b0d9713af2a5e7f3742 Mon Sep 17 00:00:00 2001 From: Guy Kogus Date: Thu, 18 Mar 2021 15:16:07 +0100 Subject: [PATCH 4/6] Remove unnecessary code --- .../json_test_example.g_non_nullable.dart | 20 ------------------- .../json_test_example.g_non_nullable.g.dart | 14 ------------- 2 files changed, 34 deletions(-) diff --git a/json_serializable/test/integration/json_test_example.g_non_nullable.dart b/json_serializable/test/integration/json_test_example.g_non_nullable.dart index a29e2b904..cb9928180 100644 --- a/json_serializable/test/integration/json_test_example.g_non_nullable.dart +++ b/json_serializable/test/integration/json_test_example.g_non_nullable.dart @@ -204,23 +204,3 @@ class UnknownEnumValue { factory UnknownEnumValue.fromJson(Map json) => _$UnknownEnumValueFromJson(json); } - -@JsonSerializable(createToJson: false) -class NullableUnknownEnumValue { - @JsonKey() - Category enumValue; - - @JsonKey() - Iterable enumIterable; - - @JsonKey() - List enumList; - - @JsonKey() - Set enumSet; - - NullableUnknownEnumValue(); - - factory NullableUnknownEnumValue.fromJson(Map json) => - _$NullableUnknownEnumValueFromJson(json); -} diff --git a/json_serializable/test/integration/json_test_example.g_non_nullable.g.dart b/json_serializable/test/integration/json_test_example.g_non_nullable.g.dart index 90f2341ce..6c0e324d6 100644 --- a/json_serializable/test/integration/json_test_example.g_non_nullable.g.dart +++ b/json_serializable/test/integration/json_test_example.g_non_nullable.g.dart @@ -201,17 +201,3 @@ UnknownEnumValue _$UnknownEnumValueFromJson(Map json) { unknownValue: Category.notDiscoveredYet)) .toSet(); } - -NullableUnknownEnumValue _$NullableUnknownEnumValueFromJson( - Map json) { - return NullableUnknownEnumValue() - ..enumValue = _$enumDecodeNullable(_$CategoryEnumMap, json['enumValue']) - ..enumIterable = (json['enumIterable'] as List) - .map((e) => _$enumDecodeNullable(_$CategoryEnumMap, e)) - ..enumList = (json['enumList'] as List) - .map((e) => _$enumDecodeNullable(_$CategoryEnumMap, e)) - .toList() - ..enumSet = (json['enumSet'] as List) - .map((e) => _$enumDecodeNullable(_$CategoryEnumMap, e)) - .toSet(); -} From 96fe8848e06b83a6249b00eb033ade9f2f0dc4f5 Mon Sep 17 00:00:00 2001 From: Guy Kogus Date: Thu, 18 Mar 2021 16:27:57 +0100 Subject: [PATCH 5/6] Clean code --- json_serializable/lib/src/type_helpers/enum_helper.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/json_serializable/lib/src/type_helpers/enum_helper.dart b/json_serializable/lib/src/type_helpers/enum_helper.dart index 4cf65c23b..f044b7175 100644 --- a/json_serializable/lib/src/type_helpers/enum_helper.dart +++ b/json_serializable/lib/src/type_helpers/enum_helper.dart @@ -117,7 +117,6 @@ T _$enumDecodeNullable( if (source == null) { return null; } - return enumValues.entries .singleWhere((e) => e.value == source, orElse: () => null) ?.key ?? unknownValue; From 276fd133898e686d4103dece4e6aa007bf574710 Mon Sep 17 00:00:00 2001 From: Guy Kogus Date: Thu, 18 Mar 2021 17:22:47 +0100 Subject: [PATCH 6/6] Only use the required enum decode helper --- json_serializable/lib/src/type_helpers/enum_helper.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/json_serializable/lib/src/type_helpers/enum_helper.dart b/json_serializable/lib/src/type_helpers/enum_helper.dart index f044b7175..a1713fac4 100644 --- a/json_serializable/lib/src/type_helpers/enum_helper.dart +++ b/json_serializable/lib/src/type_helpers/enum_helper.dart @@ -43,10 +43,10 @@ class EnumHelper extends TypeHelper { return null; } - context.addMember(_enumDecodeHelper); - if (context.nullable) { context.addMember(_enumDecodeHelperNullable); + } else { + context.addMember(_enumDecodeHelper); } context.addMember(memberContent);