Skip to content

Commit f50eb99

Browse files
authored
Fix support for Duration fields with default values (#1172)
Fixes #1170 `Duration` was overlooked when supporting default values in constructors. That's now fixed!
1 parent decbda1 commit f50eb99

13 files changed

+40
-18
lines changed

json_serializable/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 6.3.1
2+
3+
- Fixed support for `Duration` fields with default values.
4+
([#1170](https://github.com/google/json_serializable.dart/issues/1170))
5+
16
## 6.3.0
27

38
- Added support for generating `_$ExampleFieldMap`, which can be used by other

json_serializable/lib/src/type_helper.dart

-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:analyzer/dart/element/element.dart';
66
import 'package:analyzer/dart/element/type.dart';
77

88
import 'type_helpers/config_types.dart';
9-
import 'utils.dart';
109

1110
/// Context information provided in calls to [TypeHelper.serialize] and
1211
/// [TypeHelper.deserialize].
@@ -85,12 +84,3 @@ abstract class TypeHelper<T extends TypeHelperContext> {
8584
bool defaultProvided,
8685
);
8786
}
88-
89-
Object commonNullPrefix(
90-
bool nullable,
91-
String expression,
92-
Object unsafeExpression,
93-
) =>
94-
nullable
95-
? ifNullOrElse(expression, 'null', '$unsafeExpression')
96-
: unsafeExpression;

json_serializable/lib/src/type_helpers/duration_helper.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:analyzer/dart/element/type.dart';
66
import 'package:source_gen/source_gen.dart' show TypeChecker;
77
import 'package:source_helper/source_helper.dart';
88

9+
import '../default_container.dart';
910
import '../type_helper.dart';
1011

1112
class DurationHelper extends TypeHelper {
@@ -33,7 +34,7 @@ class DurationHelper extends TypeHelper {
3334
}
3435

3536
@override
36-
String? deserialize(
37+
Object? deserialize(
3738
DartType targetType,
3839
String expression,
3940
TypeHelperContext context,
@@ -43,11 +44,10 @@ class DurationHelper extends TypeHelper {
4344
return null;
4445
}
4546

46-
return commonNullPrefix(
47-
targetType.isNullableType,
47+
return DefaultContainer(
4848
expression,
4949
'Duration(microseconds: $expression as int)',
50-
).toString();
50+
);
5151
}
5252
}
5353

json_serializable/lib/src/type_helpers/to_from_string.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:analyzer/dart/element/type.dart';
66
import 'package:source_gen/source_gen.dart';
77

8-
import '../type_helper.dart';
8+
import '../utils.dart';
99

1010
final bigIntString = ToFromStringHelper(
1111
'BigInt.parse',
@@ -76,7 +76,8 @@ class ToFromStringHelper {
7676

7777
final parseParam = isString ? expression : '$expression as String';
7878

79-
return commonNullPrefix(nullable, expression, '$_parse($parseParam)')
80-
.toString();
79+
final output = '$_parse($parseParam)';
80+
81+
return nullable ? ifNullOrElse(expression, 'null', output) : output;
8182
}
8283
}

json_serializable/pubspec.yaml

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

json_serializable/test/default_value/default_value.dart

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class DefaultValue implements dvi.DefaultValue {
5959
})
6060
Map<String, List<String>> fieldMapListString;
6161

62+
Duration durationField;
63+
6264
@JsonKey(defaultValue: Greek.beta)
6365
Greek fieldEnum;
6466

@@ -83,6 +85,7 @@ class DefaultValue implements dvi.DefaultValue {
8385
this.fieldMapSimple,
8486
this.fieldMapListString,
8587
this.fieldEnum, {
88+
this.durationField = Duration.zero,
8689
this.constClass = const ConstClass('value'),
8790
this.valueFromConverter = const ConstClass('value'),
8891
this.valueFromFunction = const ConstClass('value'),

json_serializable/test/default_value/default_value.g.dart

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/default_value/default_value.g_any_map__checked.dart

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class DefaultValue implements dvi.DefaultValue {
6262
})
6363
Map<String, List<String>> fieldMapListString;
6464

65+
Duration durationField;
66+
6567
@JsonKey(defaultValue: Greek.beta)
6668
Greek fieldEnum;
6769

@@ -86,6 +88,7 @@ class DefaultValue implements dvi.DefaultValue {
8688
this.fieldMapSimple,
8789
this.fieldMapListString,
8890
this.fieldEnum, {
91+
this.durationField = Duration.zero,
8992
this.constClass = const ConstClass('value'),
9093
this.valueFromConverter = const ConstClass('value'),
9194
this.valueFromFunction = const ConstClass('value'),

json_serializable/test/default_value/default_value.g_any_map__checked.g.dart

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/default_value/default_value_interface.dart

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ abstract class DefaultValue {
2727

2828
Map<String, List<String>> get fieldMapListString;
2929

30+
Duration get durationField;
31+
3032
Greek get fieldEnum;
3133

3234
ConstClass get constClass;

json_serializable/test/default_value/default_value_test.dart

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const _defaultInstance = {
2424
'fieldMapListString': {
2525
'root': ['child']
2626
},
27+
'durationField': 0,
2728
'fieldEnum': 'beta',
2829
'constClass': {'field': 'value'},
2930
'valueFromConverter': 'value',
@@ -44,6 +45,7 @@ const _otherValues = {
4445
'fieldMapListString': {
4546
'root2': ['alpha']
4647
},
48+
'durationField': 1,
4749
'fieldEnum': 'delta',
4850
'constClass': {'field': 'otherValue'},
4951
'valueFromConverter': 'otherValue',

json_serializable/test/default_value/implicit_default_value.dart

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class DefaultValueImplicit implements dvi.DefaultValue {
3434

3535
final Map<String, List<String>> fieldMapListString;
3636

37+
final Duration durationField;
38+
3739
final Greek fieldEnum;
3840

3941
final ConstClass constClass;
@@ -59,6 +61,7 @@ class DefaultValueImplicit implements dvi.DefaultValue {
5961
'root': ['child']
6062
},
6163
this.fieldEnum = Greek.beta,
64+
this.durationField = const Duration(),
6265
this.constClass = const ConstClass('value'),
6366
this.valueFromConverter = const ConstClass('value'),
6467
this.valueFromFunction = const ConstClass('value'),

json_serializable/test/default_value/implicit_default_value.g.dart

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)