NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

C# Decimals are defined as "number" with format "decimal" which is not valid according to the OpenApi spec

Open jgoyvaerts opened this issue 3 years ago • 4 comments

Reference spec: https://swagger.io/docs/specification/data-models/data-types/#numbers

When a Decimal is defined in C#, NSwag generates a type: number, format: decimal in the swagger.json, however, this isn't a valid combination (according to the spec) and causes issues when using the swagger.json in other places (for example, https://github.com/OpenAPITools/openapi-generator can't handle this case correctly).

Is this something that's a known issue? Is there a setting I can use to make the swagger.json generator comply with the spec?

jgoyvaerts avatar Jul 15 '21 12:07 jgoyvaerts

What would be the expected output? I think you can change that with a custom type mapper or schema processor.

RicoSuter avatar Jul 31 '21 15:07 RicoSuter

I don't know what the expected output would be, some other languages/packages use strings as fallback for decimals I guess.

Why would I need to write a custom type mapper or schema processor when NSwag claims to be following the OpenApi 3.0 specification but then violates that same specification?

jgoyvaerts avatar Aug 16 '21 14:08 jgoyvaerts

Per the spec: "to support documentation needs, the format property is an open string-valued property, and can have any value. [...] Tools that do not recognize a specific format MAY default back to the type alone, as if the format is not specified.". Not being able to correctly handle that sounds like a bug in the application consuming the generated file.

C#'s decimals are more precise than singles or doubles, so neither of the pre-defined options would be correct. If limited to the pre-defined options, then format would need to be left off of the property.

hauntingEcho avatar Feb 16 '22 22:02 hauntingEcho

I think this should be configured by mapping the primitive type to the interface. The OpenApi specification is double, but in some cases I also need to map to Decimal.

image

Currently, I manually replace double to decimal after each mapping to maintain compatibility with the application's core.

fabionaspolini avatar Aug 10 '22 01:08 fabionaspolini