Skip to content

Commit

Permalink
Serializing non-nullable enums should lead to non-nullable JSON types
Browse files Browse the repository at this point in the history
fixes 1145
  • Loading branch information
ssabdb committed May 30, 2022
1 parent d5d73b8 commit fa7e302
Show file tree
Hide file tree
Showing 15 changed files with 144 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.

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

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

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

enum Issue1145RegressionEnum {
alpha,
beta,
gamma,
}

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

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

final Map<Issue1145RegressionEnum, bool> status;
}
13 changes: 13 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 fa7e302

Please sign in to comment.