Skip to content

Fix handling of nullable enum fields with includeIfNull: false #1227

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

Merged
merged 2 commits into from
Oct 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions _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.

4 changes: 4 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.5.3

- Fixed handling of nullable `enum` fields with `includeIfNull: false`.

## 6.5.2

- Better handling of `null` when encoding `enum` values or values with
Expand Down
9 changes: 7 additions & 2 deletions json_serializable/lib/src/enum_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:source_gen/source_gen.dart';
import 'package:source_helper/source_helper.dart';

import 'json_literal_generator.dart';
import 'utils.dart';
Expand All @@ -16,13 +17,17 @@ String constMapName(DartType targetType) =>

/// If [targetType] is not an enum, return `null`.
///
/// Otherwise, returns `true` if one of the encoded values of the enum is
/// `null`.
/// Otherwise, returns `true` if [targetType] is nullable OR if one of the
/// encoded values of the enum is `null`.
bool? enumFieldWithNullInEncodeMap(DartType targetType) {
final enumMap = _enumMap(targetType);

if (enumMap == null) return null;

if (targetType.isNullableType) {
return true;
}

return enumMap.values.contains(null);
}

Expand Down
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 6.5.2
version: 6.5.3
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
Expand Down
5 changes: 5 additions & 0 deletions json_serializable/test/integration/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,9 @@ void main() {
});
});
});

test('Issue1226Regression', () {
final instance = Issue1226Regression(durationType: null);
expect(instance.toJson(), isEmpty);
});
}
16 changes: 13 additions & 3 deletions json_serializable/test/integration/json_enum_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,21 @@ class Issue1145RegressionA {
createFactory: false,
)
class Issue1145RegressionB {
Issue1145RegressionB({
required this.status,
});
Issue1145RegressionB({required this.status});

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

final List<Issue1145RegressionEnum?> status;
}

@JsonSerializable(includeIfNull: false)
class Issue1226Regression {
Issue1226Regression({required this.durationType});

factory Issue1226Regression.fromJson(Map<String, dynamic> json) =>
_$Issue1226RegressionFromJson(json);

final Issue1145RegressionEnum? durationType;

Map<String, dynamic> toJson() => _$Issue1226RegressionToJson(this);
}
20 changes: 20 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.