Skip to content

Commit

Permalink
Fix epoch deserialization and parsing numbers (OpenAPITools#15948)
Browse files Browse the repository at this point in the history
* Fix 2 bugs with epoch deserialization and parsing numbers.

* Generate samples.
  • Loading branch information
noordawod authored Jun 29, 2023
1 parent e9d9866 commit d5c53b9
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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}}
Expand Down Expand Up @@ -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}}
Expand Down Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
Expand Down

0 comments on commit d5c53b9

Please sign in to comment.