Skip to content

Commit

Permalink
Fix BigInt, DateTime, Uri JsonKey.defaultValue w/ a function (#1220)
Browse files Browse the repository at this point in the history
Fixes #1219

Prepare to release v6.5.1
  • Loading branch information
kevmoo authored Oct 10, 2022
1 parent 0e89d96 commit a79c6b7
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 15 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.1

- Fixed `BigInt`, `DateTime`, and `Uri` support for `JsonKey.defaultValue` with
a function value.

## 6.5.0

- Allow constructors to be passed to `JsonKey` parameters that support
Expand Down
3 changes: 2 additions & 1 deletion json_serializable/lib/src/type_helpers/big_int_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analyzer/dart/element/type.dart';
import 'package:source_helper/source_helper.dart';

import '../default_container.dart';
import '../type_helper.dart';
import 'to_from_string.dart';

Expand All @@ -24,7 +25,7 @@ class BigIntHelper extends TypeHelper {
);

@override
String? deserialize(
DefaultContainer? deserialize(
DartType targetType,
String expression,
TypeHelperContext context,
Expand Down
3 changes: 2 additions & 1 deletion json_serializable/lib/src/type_helpers/date_time_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analyzer/dart/element/type.dart';
import 'package:source_helper/source_helper.dart';

import '../default_container.dart';
import '../type_helper.dart';
import 'to_from_string.dart';

Expand All @@ -24,7 +25,7 @@ class DateTimeHelper extends TypeHelper {
);

@override
String? deserialize(
DefaultContainer? deserialize(
DartType targetType,
String expression,
TypeHelperContext context,
Expand Down
3 changes: 2 additions & 1 deletion json_serializable/lib/src/type_helpers/map_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class MapHelper extends TypeHelper<TypeHelperContextWithConfig> {

final toFromString = _forType(keyArg);
if (toFromString != null) {
keyUsage = toFromString.deserialize(keyArg, keyUsage, false, true)!;
keyUsage =
toFromString.deserialize(keyArg, keyUsage, false, true).toString();
}

return '($expression $mapCast)$optionalQuestion.map( '
Expand Down
9 changes: 6 additions & 3 deletions json_serializable/lib/src/type_helpers/to_from_string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'package:analyzer/dart/element/type.dart';
import 'package:source_gen/source_gen.dart';

import '../utils.dart';
import '../default_container.dart';

final bigIntString = ToFromStringHelper(
'BigInt.parse',
Expand Down Expand Up @@ -64,7 +64,7 @@ class ToFromStringHelper {
return '$expression.$_toString';
}

String? deserialize(
DefaultContainer? deserialize(
DartType type,
String expression,
bool nullable,
Expand All @@ -78,6 +78,9 @@ class ToFromStringHelper {

final output = '$_parse($parseParam)';

return nullable ? ifNullOrElse(expression, 'null', output) : output;
return DefaultContainer(
expression,
output,
);
}
}
3 changes: 2 additions & 1 deletion json_serializable/lib/src/type_helpers/uri_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analyzer/dart/element/type.dart';
import 'package:source_helper/source_helper.dart';

import '../default_container.dart';
import '../type_helper.dart';
import 'to_from_string.dart';

Expand All @@ -24,7 +25,7 @@ class UriHelper extends TypeHelper {
);

@override
String? deserialize(
DefaultContainer? deserialize(
DartType targetType,
String expression,
TypeHelperContext context,
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.0
version: 6.5.1
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
Expand Down
10 changes: 10 additions & 0 deletions json_serializable/test/supported_types/input.type_bigint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ part 'input.type_bigint.g.dart';
class SimpleClass {
final BigInt value;

@JsonKey(defaultValue: _defaultValueFunc)
BigInt withDefault;

SimpleClass(
this.value,
this.withDefault,
);

factory SimpleClass.fromJson(Map<String, Object?> json) =>
Expand All @@ -24,12 +28,18 @@ class SimpleClass {
class SimpleClassNullable {
final BigInt? value;

@JsonKey(defaultValue: _defaultValueFunc)
BigInt? withDefault;

SimpleClassNullable(
this.value,
this.withDefault,
);

factory SimpleClassNullable.fromJson(Map<String, Object?> json) =>
_$SimpleClassNullableFromJson(json);

Map<String, Object?> toJson() => _$SimpleClassNullableToJson(this);
}

BigInt _defaultValueFunc() => BigInt.parse('12345');

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

10 changes: 10 additions & 0 deletions json_serializable/test/supported_types/input.type_datetime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ part 'input.type_datetime.g.dart';
class SimpleClass {
final DateTime value;

@JsonKey(defaultValue: _defaultValueFunc)
DateTime withDefault;

SimpleClass(
this.value,
this.withDefault,
);

factory SimpleClass.fromJson(Map<String, Object?> json) =>
Expand All @@ -24,12 +28,18 @@ class SimpleClass {
class SimpleClassNullable {
final DateTime? value;

@JsonKey(defaultValue: _defaultValueFunc)
DateTime? withDefault;

SimpleClassNullable(
this.value,
this.withDefault,
);

factory SimpleClassNullable.fromJson(Map<String, Object?> json) =>
_$SimpleClassNullableFromJson(json);

Map<String, Object?> toJson() => _$SimpleClassNullableToJson(this);
}

DateTime _defaultValueFunc() => DateTime.parse('2020-01-01T00:00:00.000');

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

10 changes: 10 additions & 0 deletions json_serializable/test/supported_types/input.type_uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ part 'input.type_uri.g.dart';
class SimpleClass {
final Uri value;

@JsonKey(defaultValue: _defaultValueFunc)
Uri withDefault;

SimpleClass(
this.value,
this.withDefault,
);

factory SimpleClass.fromJson(Map<String, Object?> json) =>
Expand All @@ -24,12 +28,18 @@ class SimpleClass {
class SimpleClassNullable {
final Uri? value;

@JsonKey(defaultValue: _defaultValueFunc)
Uri? withDefault;

SimpleClassNullable(
this.value,
this.withDefault,
);

factory SimpleClassNullable.fromJson(Map<String, Object?> json) =>
_$SimpleClassNullableFromJson(json);

Map<String, Object?> toJson() => _$SimpleClassNullableToJson(this);
}

Uri _defaultValueFunc() => Uri.parse('https://example.com');
8 changes: 8 additions & 0 deletions json_serializable/test/supported_types/input.type_uri.g.dart

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

Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ final _defaultInput = <String, Object?>{

final _defaultOutput = {
'value': _defaultValue,
'withDefault': _defaultValue,
};

final _nullableDefaultOutput = {
'value': null,
'withDefault': _defaultValue,
};

final _nonDefaultJson = {
'value': _altValue,
'withDefault': _altValue,
};
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ final _defaultInput = <String, Object?>{

final _defaultOutput = {
'value': _defaultValue,
'withDefault': _defaultValue,
};

final _nullableDefaultOutput = {
'value': null,
'withDefault': _defaultValue,
};

final _nonDefaultJson = {
'value': _altValue,
'withDefault': _altValue,
};
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ final _defaultInput = <String, Object?>{

final _defaultOutput = {
'value': _defaultValue,
'withDefault': _defaultValue,
};

final _nullableDefaultOutput = {
'value': null,
'withDefault': _defaultValue,
};

final _nonDefaultJson = {
'value': _altValue,
'withDefault': _altValue,
};
6 changes: 3 additions & 3 deletions json_serializable/tool/test_type_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import 'test_type_data.dart';
final _formatter = DartFormatter();

const _trivialTypesToTest = {
'BigInt': TestTypeData(
'BigInt': TestTypeData.defaultFunc(
jsonExpression: "'12345'",
altJsonExpression: "'67890'",
),
'bool': TestTypeData(
defaultExpression: 'true',
altJsonExpression: 'false',
),
'DateTime': TestTypeData(
'DateTime': TestTypeData.defaultFunc(
jsonExpression: "'2020-01-01T00:00:00.000'",
altJsonExpression: "'2018-01-01T00:00:00.000'",
),
Expand Down Expand Up @@ -56,7 +56,7 @@ const _trivialTypesToTest = {
defaultExpression: "'a string'",
altJsonExpression: "'another string'",
),
'Uri': TestTypeData(
'Uri': TestTypeData.defaultFunc(
jsonExpression: "'https://example.com'",
altJsonExpression: "'https://dart.dev'",
),
Expand Down
Loading

0 comments on commit a79c6b7

Please sign in to comment.