10

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

AndrewSilver
  • 976
  • 2
  • 14
  • 25
abc
  • 2,285
  • 5
  • 29
  • 64

1 Answers1

12

Try adding this line into your .AddSwaggerGen() definition

services.AddSwaggerGen(c =>
  c.MapType<decimal>(() => new OpenApiSchema { Type = "number", Format = "decimal" });
  // ...
AndrewSilver
  • 976
  • 2
  • 14
  • 25
  • 1
    And if the domain type is decimal?, use c.MapType(() => new OpenApiSchema { Type = "number", Format="decimal", Nullable = true }); – Pat Jul 23 '23 at 19:37