mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-03-10 15:51:38 +00:00
Fix epoch deserialization and parsing numbers (#15948)
* Fix 2 bugs with epoch deserialization and parsing numbers. * Generate samples.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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/';
|
||||
|
||||
@@ -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}}
|
||||
|
||||
@@ -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/';
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
@@ -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/';
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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'),
|
||||
|
||||
@@ -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']),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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']),
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
@@ -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'),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user