Fix epoch deserialization and parsing numbers (#15948)

* Fix 2 bugs with epoch deserialization and parsing numbers.

* Generate samples.
This commit is contained in:
Noor Dawod
2023-06-29 18:06:12 +02:00
committed by GitHub
parent e9d98666a1
commit d5c53b9812
16 changed files with 28 additions and 32 deletions

View File

@@ -86,7 +86,7 @@ DateTime? mapDateTime(dynamic map, String key, [String? pattern]) {
if (value is int) {
millis = value;
} else if (value is String) {
if (pattern == _dateEpochMarker) {
if (_isEpochMarker(pattern)) {
millis = int.tryParse(value);
} else {
return DateTime.tryParse(value);

View File

@@ -31,3 +31,5 @@ final _regSet = RegExp(r'^Set<(.*)>$');
final _regMap = RegExp(r'^Map<String,(.*)>$');
ApiClient defaultApiClient = ApiClient();
bool _isEpochMarker(String? pattern) => pattern == _dateEpochMarker || pattern == '/$_dateEpochMarker/';

View File

@@ -66,7 +66,7 @@ class {{{classname}}} {
{{/isNullable}}
{{#isDateTime}}
{{#pattern}}
json[r'{{{baseName}}}'] = _dateEpochMarker == '{{{pattern}}}'
json[r'{{{baseName}}}'] = _isEpochMarker(r'{{{pattern}}}')
? this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.millisecondsSinceEpoch
: this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc().toIso8601String();
{{/pattern}}
@@ -76,7 +76,7 @@ class {{{classname}}} {
{{/isDateTime}}
{{#isDate}}
{{#pattern}}
json[r'{{{baseName}}}'] = _dateEpochMarker == '{{{pattern}}}'
json[r'{{{baseName}}}'] = _isEpochMarker(r'{{{pattern}}}')
? this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.millisecondsSinceEpoch
: _dateFormatter.format(this.{{{name}}}{{#isNullable}}!{{/isNullable}}{{^isNullable}}{{^required}}{{^defaultValue}}!{{/defaultValue}}{{/required}}{{/isNullable}}.toUtc());
{{/pattern}}
@@ -128,10 +128,10 @@ class {{{classname}}} {
return {{{classname}}}(
{{#vars}}
{{#isDateTime}}
{{{name}}}: mapDateTime(json, r'{{{baseName}}}', '{{{pattern}}}'){{#required}}{{^isNullable}}!{{/isNullable}}{{/required}}{{^required}}{{#defaultValue}} ?? {{{.}}}{{/defaultValue}}{{/required}},
{{{name}}}: mapDateTime(json, r'{{{baseName}}}', r'{{{pattern}}}'){{#required}}{{^isNullable}}!{{/isNullable}}{{/required}}{{^required}}{{#defaultValue}} ?? {{{.}}}{{/defaultValue}}{{/required}},
{{/isDateTime}}
{{#isDate}}
{{{name}}}: mapDateTime(json, r'{{{baseName}}}', '{{{pattern}}}'){{#required}}{{^isNullable}}!{{/isNullable}}{{/required}}{{^required}}{{#defaultValue}} ?? {{{.}}}{{/defaultValue}}{{/required}},
{{{name}}}: mapDateTime(json, r'{{{baseName}}}', r'{{{pattern}}}'){{#required}}{{^isNullable}}!{{/isNullable}}{{/required}}{{^required}}{{#defaultValue}} ?? {{{.}}}{{/defaultValue}}{{/required}},
{{/isDate}}
{{^isDateTime}}
{{^isDate}}
@@ -211,9 +211,9 @@ class {{{classname}}} {
{{/isMap}}
{{^isMap}}
{{#isNumber}}
{{{name}}}: json[r'{{{baseName}}}'] == null
{{{name}}}: {{#isNullable}}json[r'{{{baseName}}}'] == null
? {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}
: {{{datatypeWithEnum}}}.parse(json[r'{{{baseName}}}'].toString()),
: {{/isNullable}}{{{datatypeWithEnum}}}.parse('${json[r'{{{baseName}}}']}'),
{{/isNumber}}
{{^isNumber}}
{{^isEnum}}

View File

@@ -47,3 +47,5 @@ final _regSet = RegExp(r'^Set<(.*)>$');
final _regMap = RegExp(r'^Map<String,(.*)>$');
ApiClient defaultApiClient = ApiClient();
bool _isEpochMarker(String? pattern) => pattern == _dateEpochMarker || pattern == '/$_dateEpochMarker/';

View File

@@ -87,7 +87,7 @@ DateTime? mapDateTime(dynamic map, String key, [String? pattern]) {
if (value is int) {
millis = value;
} else if (value is String) {
if (pattern == _dateEpochMarker) {
if (_isEpochMarker(pattern)) {
millis = int.tryParse(value);
} else {
return DateTime.tryParse(value);

View File

@@ -133,7 +133,7 @@ class Order {
id: mapValueOfType<int>(json, r'id'),
petId: mapValueOfType<int>(json, r'petId'),
quantity: mapValueOfType<int>(json, r'quantity'),
shipDate: mapDateTime(json, r'shipDate', ''),
shipDate: mapDateTime(json, r'shipDate', r''),
status: OrderStatusEnum.fromJson(json[r'status']),
complete: mapValueOfType<bool>(json, r'complete') ?? false,
);

View File

@@ -92,3 +92,5 @@ final _regSet = RegExp(r'^Set<(.*)>$');
final _regMap = RegExp(r'^Map<String,(.*)>$');
ApiClient defaultApiClient = ApiClient();
bool _isEpochMarker(String? pattern) => pattern == _dateEpochMarker || pattern == '/$_dateEpochMarker/';

View File

@@ -105,7 +105,7 @@ DateTime? mapDateTime(dynamic map, String key, [String? pattern]) {
if (value is int) {
millis = value;
} else if (value is String) {
if (pattern == _dateEpochMarker) {
if (_isEpochMarker(pattern)) {
millis = int.tryParse(value);
} else {
return DateTime.tryParse(value);

View File

@@ -71,9 +71,7 @@ class FakeBigDecimalMap200Response {
}());
return FakeBigDecimalMap200Response(
someId: json[r'someId'] == null
? null
: num.parse(json[r'someId'].toString()),
someId: num.parse('${json[r'someId']}'),
someMap: mapCastOfType<String, num>(json, r'someMap') ?? const {},
);
}

View File

@@ -280,17 +280,15 @@ class FormatTest {
integer: mapValueOfType<int>(json, r'integer'),
int32: mapValueOfType<int>(json, r'int32'),
int64: mapValueOfType<int>(json, r'int64'),
number: json[r'number'] == null
? null
: num.parse(json[r'number'].toString()),
number: num.parse('${json[r'number']}'),
float: mapValueOfType<double>(json, r'float'),
double_: mapValueOfType<double>(json, r'double'),
decimal: mapValueOfType<double>(json, r'decimal'),
string: mapValueOfType<String>(json, r'string'),
byte: mapValueOfType<String>(json, r'byte')!,
binary: null, // No support for decoding binary content from JSON
date: mapDateTime(json, r'date', '')!,
dateTime: mapDateTime(json, r'dateTime', ''),
date: mapDateTime(json, r'date', r'')!,
dateTime: mapDateTime(json, r'dateTime', r''),
uuid: mapValueOfType<String>(json, r'uuid'),
password: mapValueOfType<String>(json, r'password')!,
patternWithDigits: mapValueOfType<String>(json, r'pattern_with_digits'),

View File

@@ -88,7 +88,7 @@ class MixedPropertiesAndAdditionalPropertiesClass {
return MixedPropertiesAndAdditionalPropertiesClass(
uuid: mapValueOfType<String>(json, r'uuid'),
dateTime: mapDateTime(json, r'dateTime', ''),
dateTime: mapDateTime(json, r'dateTime', r''),
map: Animal.mapFromJson(json[r'map']),
);
}

View File

@@ -164,11 +164,11 @@ class NullableClass {
integerProp: mapValueOfType<int>(json, r'integer_prop'),
numberProp: json[r'number_prop'] == null
? null
: num.parse(json[r'number_prop'].toString()),
: num.parse('${json[r'number_prop']}'),
booleanProp: mapValueOfType<bool>(json, r'boolean_prop'),
stringProp: mapValueOfType<String>(json, r'string_prop'),
dateProp: mapDateTime(json, r'date_prop', ''),
datetimeProp: mapDateTime(json, r'datetime_prop', ''),
dateProp: mapDateTime(json, r'date_prop', r''),
datetimeProp: mapDateTime(json, r'datetime_prop', r''),
arrayNullableProp: Object.listFromJson(json[r'array_nullable_prop']),
arrayAndItemsNullableProp: Object.listFromJson(json[r'array_and_items_nullable_prop']),
arrayItemsNullable: Object.listFromJson(json[r'array_items_nullable']),

View File

@@ -65,9 +65,7 @@ class NumberOnly {
}());
return NumberOnly(
justNumber: json[r'JustNumber'] == null
? null
: num.parse(json[r'JustNumber'].toString()),
justNumber: num.parse('${json[r'JustNumber']}'),
);
}
return null;

View File

@@ -104,9 +104,7 @@ class ObjectWithDeprecatedFields {
return ObjectWithDeprecatedFields(
uuid: mapValueOfType<String>(json, r'uuid'),
id: json[r'id'] == null
? null
: num.parse(json[r'id'].toString()),
id: num.parse('${json[r'id']}'),
deprecatedRef: DeprecatedObject.fromJson(json[r'deprecatedRef']),
bars: json[r'bars'] is Iterable
? (json[r'bars'] as Iterable).cast<String>().toList(growable: false)

View File

@@ -133,7 +133,7 @@ class Order {
id: mapValueOfType<int>(json, r'id'),
petId: mapValueOfType<int>(json, r'petId'),
quantity: mapValueOfType<int>(json, r'quantity'),
shipDate: mapDateTime(json, r'shipDate', ''),
shipDate: mapDateTime(json, r'shipDate', r''),
status: OrderStatusEnum.fromJson(json[r'status']),
complete: mapValueOfType<bool>(json, r'complete') ?? false,
);

View File

@@ -97,9 +97,7 @@ class OuterComposite {
}());
return OuterComposite(
myNumber: json[r'my_number'] == null
? null
: num.parse(json[r'my_number'].toString()),
myNumber: num.parse('${json[r'my_number']}'),
myString: mapValueOfType<String>(json, r'my_string'),
myBoolean: mapValueOfType<bool>(json, r'my_boolean'),
);