Skip to content

Unsupported enum values #839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions json_serializable/lib/src/type_helpers/enum_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {
return null;
}

context.addMember(_enumDecodeHelper);

if (context.nullable) {
context.addMember(_enumDecodeHelperNullable);
} else {
context.addMember(_enumDecodeHelper);
}

context.addMember(memberContent);
Expand Down Expand Up @@ -96,14 +96,15 @@ T _$enumDecode<T>(
}

final value = enumValues.entries
.singleWhere((e) => e.value == source, orElse: () => null)
?.key;
.singleWhere((e) => e.value == source, orElse: () => null)
?.key ??
unknownValue;

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;
}
''';

Expand All @@ -116,5 +117,7 @@ T _$enumDecodeNullable<T>(
if (source == null) {
return null;
}
return _$enumDecode<T>(enumValues, source, unknownValue: unknownValue);
return enumValues.entries
.singleWhere((e) => e.value == source, orElse: () => null)
?.key ?? unknownValue;
}''';
14 changes: 14 additions & 0 deletions json_serializable/test/integration/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
}
20 changes: 20 additions & 0 deletions json_serializable/test/integration/json_test_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,23 @@ class UnknownEnumValue {
factory UnknownEnumValue.fromJson(Map<String, dynamic> json) =>
_$UnknownEnumValueFromJson(json);
}

@JsonSerializable(createToJson: false)
class NullableUnknownEnumValue {
@JsonKey()
Category enumValue;

@JsonKey()
Iterable<Category> enumIterable;

@JsonKey()
List<Category> enumList;

@JsonKey()
Set<Category> enumSet;

NullableUnknownEnumValue();

factory NullableUnknownEnumValue.fromJson(Map<String, dynamic> json) =>
_$NullableUnknownEnumValueFromJson(json);
}
20 changes: 19 additions & 1 deletion json_serializable/test/integration/json_test_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.