Skip to content

Commit

Permalink
Serializing enums explicitly references types unless nullable (issue … (
Browse files Browse the repository at this point in the history
#1146)

Before this change, serializing non-nullable enums in a subtype was incorrectly producing nullable string types.

For example, serializing a class member of type Map<enum, String> produced a type Map<String?, dynamic> where Map<String, dynamic> should have been produced.

Nullable enums should still produce nullable types, eg. serializing List<enum?> should produce List<String?>

fixes #1145
  • Loading branch information
ssabdb committed Jun 25, 2022
1 parent 4982848 commit 0f383e7
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 83 deletions.
2 changes: 1 addition & 1 deletion _test_yaml/test/src/build_config.g.dart

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

2 changes: 2 additions & 0 deletions json_serializable/CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@
of type `MyClass?`. ([#822](https://github.com/google/json_serializable.dart/issues/822))
- Added support for `JsonSerializable(converters: <JsonConverter>[])`
([#1072](https://github.com/google/json_serializable.dart/issues/1072))
- Fix issue with serialization of non-nullable enumerations emitting a nullable
type ([#1146](https://github.com/google/json_serializable.dart/pull/1146))

## 6.2.0

Expand Down
6 changes: 5 additions & 1 deletion json_serializable/lib/src/type_helpers/enum_helper.dart
Expand Up @@ -29,7 +29,11 @@ class EnumHelper extends TypeHelper<TypeHelperContextWithConfig> {

context.addMember(memberContent);

return '${constMapName(targetType)}[$expression]';
if (targetType.isNullableType) {
return '${constMapName(targetType)}[$expression]';
} else {
return '${constMapName(targetType)}[$expression]!';
}
}

@override
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/test/default_value/default_value.g.dart

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

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

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

17 changes: 17 additions & 0 deletions json_serializable/test/integration/integration_test.dart
Expand Up @@ -304,6 +304,23 @@ void main() {
expect(dayTypeEnumValues, ['no-good', 'rotten', 'very-bad']);
});

test(
'serializing a non-nullable enum as a key in a map should produce a '
'non-nullable string key', () {
final cls =
Issue1145RegressionA(status: {Issue1145RegressionEnum.gamma: true});
// Due to issue 1145 this resulted in Map<String?, dynamic>
expect(cls.toJson()['status'], const TypeMatcher<Map<String, dynamic>>());
});

test(
'serializing a nullable enum in a list should produce a list with'
' nullable entries', () {
final cls = Issue1145RegressionB(status: [Issue1145RegressionEnum.gamma]);
// Issue 1145 should not have affected nullable enums
expect(cls.toJson()['status'], const TypeMatcher<List<String?>>());
});

test('unknown as null for enum', () {
expect(
() => Issue559Regression.fromJson({}).status,
Expand Down
32 changes: 32 additions & 0 deletions json_serializable/test/integration/json_enum_example.dart
Expand Up @@ -49,3 +49,35 @@ enum Issue559RegressionEnum {
beta,
gamma,
}

enum Issue1145RegressionEnum {
alpha,
beta,
gamma,
}

@JsonSerializable(
createFactory: false,
)
class Issue1145RegressionA {
Issue1145RegressionA({
required this.status,
});

Map<String, dynamic> toJson() => _$Issue1145RegressionAToJson(this);

final Map<Issue1145RegressionEnum, bool> status;
}

@JsonSerializable(
createFactory: false,
)
class Issue1145RegressionB {
Issue1145RegressionB({
required this.status,
});

Map<String, dynamic> toJson() => _$Issue1145RegressionBToJson(this);

final List<Issue1145RegressionEnum?> status;
}
21 changes: 21 additions & 0 deletions json_serializable/test/integration/json_enum_example.g.dart

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

6 changes: 3 additions & 3 deletions 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.

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

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

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

4 changes: 2 additions & 2 deletions json_serializable/test/supported_types/input.type_list.g.dart

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

0 comments on commit 0f383e7

Please sign in to comment.