Skip to content

Commit

Permalink
Prevent serialization of non-nullable enums using _$xxxEnumMap from i…
Browse files Browse the repository at this point in the history
…ncorrectly producing

nullable string types.

For example, before this change 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 May 31, 2022
1 parent d5d73b8 commit ff128db
Show file tree
Hide file tree
Showing 15 changed files with 173 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.

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});
// Prior to issue 1145 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]);
// Prior to issue 1145 resulted in Map<String?, dynamic>
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 ff128db

Please sign in to comment.