Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 227da7a

Browse files
authoredOct 19, 2022
Fix handling of nullable enum fields with includeIfNull: false (google#1227)
Fixes google#1226
1 parent fed0f2e commit 227da7a

File tree

7 files changed

+53
-9
lines changed

7 files changed

+53
-9
lines changed
 

‎_test_yaml/test/src/build_config.g.dart

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎json_serializable/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.5.3
2+
3+
- Fixed handling of nullable `enum` fields with `includeIfNull: false`.
4+
15
## 6.5.2
26

37
- Better handling of `null` when encoding `enum` values or values with

‎json_serializable/lib/src/enum_utils.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analyzer/dart/element/element.dart';
77
import 'package:analyzer/dart/element/type.dart';
88
import 'package:json_annotation/json_annotation.dart';
99
import 'package:source_gen/source_gen.dart';
10+
import 'package:source_helper/source_helper.dart';
1011

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

1718
/// If [targetType] is not an enum, return `null`.
1819
///
19-
/// Otherwise, returns `true` if one of the encoded values of the enum is
20-
/// `null`.
20+
/// Otherwise, returns `true` if [targetType] is nullable OR if one of the
21+
/// encoded values of the enum is `null`.
2122
bool? enumFieldWithNullInEncodeMap(DartType targetType) {
2223
final enumMap = _enumMap(targetType);
2324

2425
if (enumMap == null) return null;
2526

27+
if (targetType.isNullableType) {
28+
return true;
29+
}
30+
2631
return enumMap.values.contains(null);
2732
}
2833

‎json_serializable/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 6.5.2
2+
version: 6.5.3
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.

‎json_serializable/test/integration/integration_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,9 @@ void main() {
414414
});
415415
});
416416
});
417+
418+
test('Issue1226Regression', () {
419+
final instance = Issue1226Regression(durationType: null);
420+
expect(instance.toJson(), isEmpty);
421+
});
417422
}

‎json_serializable/test/integration/json_enum_example.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,21 @@ class Issue1145RegressionA {
8686
createFactory: false,
8787
)
8888
class Issue1145RegressionB {
89-
Issue1145RegressionB({
90-
required this.status,
91-
});
89+
Issue1145RegressionB({required this.status});
9290

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

9593
final List<Issue1145RegressionEnum?> status;
9694
}
95+
96+
@JsonSerializable(includeIfNull: false)
97+
class Issue1226Regression {
98+
Issue1226Regression({required this.durationType});
99+
100+
factory Issue1226Regression.fromJson(Map<String, dynamic> json) =>
101+
_$Issue1226RegressionFromJson(json);
102+
103+
final Issue1145RegressionEnum? durationType;
104+
105+
Map<String, dynamic> toJson() => _$Issue1226RegressionToJson(this);
106+
}

‎json_serializable/test/integration/json_enum_example.g.dart

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.