domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.17k stars 1.28k forks source link

No schema/$ref for [FromForm] arguments in multipart/form-data requests #2439

Open magicxor opened 2 years ago

magicxor commented 2 years ago

Hello. I'm trying to generate named models (arguments) for multipart/form-data requests:

namespace ReproApp.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        [HttpPost("create1")]
        [Consumes("multipart/form-data")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task<IActionResult> Create1([FromForm] WeatherForecast weatherForecast)
        {
            return Ok();
        }

        [HttpPost("create2")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task<IActionResult> Create2([FromBody] WeatherForecast weatherForecast)
        {
            return Ok();
        }
    }
}

It works for [FromBody] arguments, but it doesn't work for [FromForm] arguments:

{
"/WeatherForecast/create2": {
      "post": {
        "tags": [
          "WeatherForecast"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/WeatherForecast"
              }
            },
            "text/json": {
              "schema": {
                "$ref": "#/components/schemas/WeatherForecast"
              }
            },
            "application/*+json": {
              "schema": {
                "$ref": "#/components/schemas/WeatherForecast"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }
}
 "/WeatherForecast/create1": {
      "post": {
        "tags": [
          "WeatherForecast"
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "Date": {
                    "type": "string",
                    "format": "date-time"
                  },
                  "TemperatureC": {
                    "type": "integer",
                    "format": "int32"
                  },
                  "TemperatureF": {
                    "type": "integer",
                    "format": "int32"
                  },
                  "Summary": {
                    "type": "string"
                  }
                }
              },
              "encoding": {
                "Date": {
                  "style": "form"
                },
                "TemperatureC": {
                  "style": "form"
                },
                "TemperatureF": {
                  "style": "form"
                },
                "Summary": {
                  "style": "form"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }

Is there any workaround? I would like to get something like

       content:
         multipart/form-data:
           schema:
             $ref: '#/components/schemas/WeatherForecast'

ReproApp.zip

I'm using Swashbuckle.AspNetCore 6.2.3 on net6.0.

Rwhalestorm commented 1 year ago

I'm also looking for this. Is there a way to do this or a easy work around?

ixnas commented 1 year ago

Also looking for a workaround here.

I know that ASP's ApiExplorer exposes limited information on [FromForm]-annotated parameters and limits schema generation (issue, issue). But a workaround to get a $ref and a schema would be incredibly helpful for front-end type generation.

Even being able to manually annotate the controller method with a schema name or something would help.