gibahjoe / openapi-generator-dart

Openapi Generator for Dart/Flutter
BSD 3-Clause "New" or "Revised" License
128 stars 34 forks source link

Parsing Double values without decimals #35

Closed mikthemonster closed 3 years ago

mikthemonster commented 3 years ago

Hi,

My API (.net core 5.0) sometimes returns double values without the decimal point:

"totalPrice": 1

instead of:

totalPrice": 1.0

Unfortunately this gives an error:

type 'int' is not a subtype of type 'double'

Can I somehow fix this without changing the auto-generated files?

gibahjoe commented 3 years ago

You can't try using the type mappings field to map int and double to num.

However, this is going to apply it throughout the generated code.

Other option will be to ensure the api returns the right type always

mikthemonster commented 3 years ago

You can't try using the type mappings field to map int and double to num.

Hmmm, im not sure how to do that... Do you have an example or can you guide me?

The error is related to this line of autogenerated code:

totalPrice: json[r'totalPrice'],

It happens when totalPrice does not contain a "." (default behavior in .net core 5 when the double value does not contain any decimals)

gibahjoe commented 3 years ago

The openapi annotation has a typeMappings property that takes a map of string. Add a mapping for int and double to num

@Openapi(
...
typeMappings:{'int':'num','double':'num'}
...
) 
mikthemonster commented 3 years ago
@Openapi(
...
typeMappings:{'int':'num','double':'num'}
...
) 

Thanks, its working! :-)