RicoSuter / NSwag

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

Version 14 - FromBody enums now treated as strings #4727

Open PeeDeeWhite opened 7 months ago

PeeDeeWhite commented 7 months ago

Hi

We have a number of controllers with actions that post an enum and that enum is taken from the body of the request. For example the following test action demonstrates the issue

public async Task<ActionResult> TestEnum([FromBody] MyEnum value)

Prior to v14 the swagger generated would have the request body content set to the enum type but with version 14 it is now treated as a string as shown below.

Is this an intended breaking change? The workaround is to removing the FromBody attribute but this has a significant impact for us.

v13.x

"requestBody": {
          "x-name": "value",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/MyEnum"
              }
            }
          },

v14

"requestBody": {
          "x-name": "value",
          "content": {
            "application/json": {
              "schema": {
                "type": "string"
              }
            }
          },
AntonioSCoelho commented 6 months ago

We were facing the same issue.

RicoSuter commented 6 months ago

Seems to be a regression.

RicoSuter commented 6 months ago

Seems to be a regression with .NET 8

.NET 8 reports a string:

image

.NET 7 an enum (correct)

image

A "fix" would be to use the [JsonSchemaType] attribute for now:

public void Post([FromBody, JsonSchemaType(typeof(TestEnum))] TestEnum asdf)
serhii-trush commented 2 months ago

Thanks for providing a workaround! In my case it was failing one of the properties bound with [FromForm], I guess the reason is the same.

Do we expect a fix for any time in the foreseeable future ?

WeberEric commented 1 month ago

refresh topic, i have the same problem. Is there a better fix for that?