Skip to content

Incorrect schema generation for IFormFile parameters in Minimal API projects when using WithOpenApi extension method #2625

@marcominerva

Description

@marcominerva

I'm using .NET 7.0.4 and Swashbuckle.AspNetCore 6.5.0.

Consider the following endpoints:

app.MapPost("/api/upload", (IFormFile file) =>
{
    return TypedResults.Ok(file.FileName);
});

app.MapPost("/api/upload_with_openapi", (IFormFile file) =>
{
    return TypedResults.Ok(file.FileName);
})
.WithOpenApi();

The first one generates the following schema:

schema": {
  "required": [
    "file"
  ],
  "type": "object",
  "properties": {
    "file": {
      "type": "string",
      "format": "binary"
    }
  }
},

While the second, using WithOpenApi, produces this:

"schema": {
  "type": "string",
  "format": "binary"
},

This causes an exception when calling the /api/upload_with_openapi endpoint:

Microsoft.AspNetCore.Http.BadHttpRequestException: Required parameter "IFormFile file" was not provided from form file.
       at lambda_method3(Closure, Object, HttpContext, Object)
       at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass92_2.<<HandleRequestBodyAndCompileRequestDelegateForForm>b__2>d.MoveNext()
    --- End of stack trace from previous location ---
       at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
       at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
       at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
       at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

You can find a minimal repro here: https://github.com/marcominerva/FileUploadIssue

Original issue posted at dotnet/aspnetcore#47526

Metadata

Metadata

Assignees

No one assigned

    Labels

    help-wantedA change up for grabs for contributions from the community

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions