Description
Description
Suppose you have an API Model containing a double property, swagger-codegen will generate a class similar to this:
class ApiResponse {
double ttl = null;
LoginResponse.fromJson(Map<String, dynamic> json) {
if (json == null) return;
ttl =
json['ttl']
;
}
}
This works fine, as long as the JSON returned from the API includes a decimal place, for example:
{
"ttl": 21.1
}
However, If the API returns a value that contains no decimal place, Dart's JsonCodec
will parse this as an int
. The Model's fromJson
method will then throw an exception, as an int
cannot be assigned to a double
.
An API returning the following:
{
"ttl": 21
}
Will be parsed by JsonCodec
an int, and will then throw the following exception when attempting to assign it to ApiModel.ttl
:
_TypeError (type 'int' is not a subtype of type 'double')
Swagger-codegen version
2.48 (compiled from 31e7ea9)
Swagger declaration file content or url
N/A
Command line used for generation
N/A
Steps to reproduce
N/A
Related issues/PRs
N/A
Suggest a fix/enhancement
To assign to a double, the value parsed from the json should be explicitly cast to a double before assigning. This works as all Dart's number types have a toDouble
method.