Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

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

Open magicxor opened this issue 3 years ago • 2 comments

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.

magicxor avatar Jun 12 '22 09:06 magicxor

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

Rwhalestorm avatar Sep 22 '22 02:09 Rwhalestorm

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.

ixnas avatar Jan 10 '23 11:01 ixnas