Skip to content
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

Serializing enums explicitly references types unless nullable (issue … #1146

Merged
merged 4 commits into from Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>>());
ssabdb marked this conversation as resolved.
Show resolved Hide resolved
});

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.

2 changes: 1 addition & 1 deletion json_serializable/test/supported_types/extra_map_test.dart
Expand Up @@ -3,10 +3,10 @@
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: prefer_const_declarations
@TestOn('vm')

import 'dart:convert';

@TestOn('vm')
import 'package:test/test.dart';

import '../test_utils.dart';
Expand Down

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.