Skip to content

[Minimal API] IFormFile does not bind when using WithOpenApi extension method #47526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
marcominerva opened this issue Apr 1, 2023 · 4 comments
Closed
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc bug This issue describes a behavior which is not expected - a bug.

Comments

@marcominerva
Copy link
Contributor

marcominerva commented Apr 1, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm using .NET 7.0.4. I have defined 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();

When I invoke the first one, everything works as expected. However, when I call the endpoint that uses WithOpenApi, I get an exception:

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)

I have also tried adding the [FromForm] attribute to the IFormFile parameter, but with no luck.

Expected Behavior

IFormFile should work even when using the WithOpenApi extension method.

Steps To Reproduce

Minimal repro: https://github.com/marcominerva/FileUploadIssue

Exceptions (if any)

No response

.NET Version

7.0.202

Anything else?

No response

@ghost ghost added the area-runtime label Apr 1, 2023
@mitchdenny
Copy link
Member

This does look like a bug. Thanks for reporting it!

@mitchdenny mitchdenny added the bug This issue describes a behavior which is not expected - a bug. label Apr 3, 2023
@captainsafia
Copy link
Member

Without the WithOpenApi call, the schema that is produced looks as follows:

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

With the call invoked, the schema looks as follows:

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

Since we still rely on the schema generation logic in Swashbuckle (ref), I suspect the bug is in the fact that we don't invoke the GenerateSchemaFromFormParameters method in the schema override logic.

We'll have to fix this bug over on the Swashbuckle side for now.

@marcominerva Do you mind filing this issue in the Swashbuckle.AspNetCore repo?

@marcominerva
Copy link
Contributor Author

Sure @captainsafia, done :) Thank you for your support!

@mitchdenny
Copy link
Member

Closing this bug here now that there is one tracking it on the Swashbuckle side.

@mitchdenny mitchdenny added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-runtime labels Apr 5, 2023
@ghost ghost locked as resolved and limited conversation to collaborators May 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

No branches or pull requests

3 participants