RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.69k stars 1.24k forks source link

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

Open jgoyvaerts opened 3 years ago

jgoyvaerts commented 3 years ago

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?

RicoSuter commented 3 years ago

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

jgoyvaerts commented 3 years ago

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?

hauntingEcho commented 2 years ago

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.

fabionaspolini commented 2 years ago

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.