Swagger/NSwag keep decimal datatype
Asked Answered
R

1

11

I do have my .net data classes, containing a few decimal fields (for example quantity). I generate an openapi.json out of it running dotnet swagger.

...
"quantity": {
            "type": "number",
            "format": "double"
          },
...

As you can see it produces a type "number" with format "double". And nswag creates a client, where the field is a double too.

Can I configure somehow to set format as decimal? When I edit the openapi.json manually, the nswag creation produces a decimal as expected.

I tried to add this annotation, but it doesn't change anything: [JsonSchema(JsonObjectType.Number, Format = "decimal")]

Rusticate answered 11/10, 2021 at 9:8 Comment(0)
B
14

Try adding this line into your .AddSwaggerGen() definition

services.AddSwaggerGen(c =>
  c.MapType<decimal>(() => new OpenApiSchema { Type = "number", Format = "decimal" });
  // ...
Beadruby answered 11/10, 2021 at 18:59 Comment(1)
And if the domain type is decimal?, use c.MapType<decimal?>(() => new OpenApiSchema { Type = "number", Format="decimal", Nullable = true });Hernando

© 2022 - 2024 — McMap. All rights reserved.