Skip to content

Commit

Permalink
Fix encoding nullable values with converters, prepare for release (#1231
Browse files Browse the repository at this point in the history
)

Fixes #1229
  • Loading branch information
kevmoo authored Oct 25, 2022
1 parent 227da7a commit 2cfe92a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
5 changes: 5 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 6.5.4

- Fixed handling of nullable fields with converters which return non-nullable
values.

## 6.5.3

- Fixed handling of nullable `enum` fields with `includeIfNull: false`.
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/lib/src/encoder_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ abstract class EncodeHelper implements HelperCore {
hasConverterNullEncode(field.type, helperContext);

if (nullableEncodeConverter != null) {
return !nullableEncodeConverter;
return !nullableEncodeConverter && !field.type.isNullableType;
}

// We can consider enums as kinda like having custom converters
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.3
version: 6.5.4
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
Expand Down
24 changes: 24 additions & 0 deletions json_serializable/test/integration/converter_examples.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,27 @@ class _Issue1202RegressionNotNullConverter extends JsonConverter<int, String> {
@override
String toJson(int object) => object.toString();
}

class DateTimeConverter extends JsonConverter<DateTime, String> {
const DateTimeConverter();
@override
DateTime fromJson(String json) => DateTime.parse(json).toLocal();
@override
String toJson(DateTime object) => object.toUtc().toIso8601String();
}

@JsonSerializable()
@DateTimeConverter()
class Regression1229 {
@JsonKey(includeIfNull: false)
final DateTime? date;

Regression1229({
this.date,
});

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

Map<String, dynamic> toJson() => _$Regression1229ToJson(this);
}
34 changes: 34 additions & 0 deletions json_serializable/test/integration/converter_examples.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -419,4 +419,9 @@ void main() {
final instance = Issue1226Regression(durationType: null);
expect(instance.toJson(), isEmpty);
});

test('Regression1229', () {
final instance = Regression1229();
expect(instance.toJson(), isEmpty);
});
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2cfe92a

Please sign in to comment.