Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger/NSwag keep decimal datatype

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")]

like image 366
abc Avatar asked Jan 27 '26 08:01

abc


1 Answers

Try adding this line into your .AddSwaggerGen() definition

services.AddSwaggerGen(c =>
  c.MapType<decimal>(() => new OpenApiSchema { Type = "number", Format = "decimal" });
  // ...
like image 93
AndrewSilver Avatar answered Jan 29 '26 00:01

AndrewSilver