Adil
Adil

Reputation: 231

File upload with .net core minimal API, Swagger not showing parameter name

I've created .net core minimal API to upload file using .net 7, Swashbuckle 6.5. API working without Swagger and tested with Postman.

Adding OpenAPI/Swagger support with Swashbuckle, it generates test page but file upload parameter name it doesn't show in Swagger and trying to upload file without name cause following error.

The issue is reported in Swashbuckle GitHub issue tracker. https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2625

Is there any workaround for it

Microsoft.AspNetCore.Http.BadHttpRequestException: Required parameter "IFormFile file" was not provided from form file.

Swagger screen

enter image description here

app.MapPost("/ReadPDF", async (IFormFile file) =>
{
    string tempfile = CreateTempfilePath();
    using var stream = File.OpenWrite(tempfile);

    await file.CopyToAsync(stream);
    
    stream.Close();

    StringBuilder sbPDFText=new StringBuilder();
    

    return Text.ToString();

})
    .WithName("ReadPDF")
.WithOpenApi(operation => new(operation)
{

    Summary = "Reads text from PDF",
    Description = "This API returns all text in PDF document"
});
;





I already trying solution mentioned on the issue GitHub page as workaround but couldn't figure out. https://github.com/dotnet/aspnetcore/issues/47692

Upvotes: 5

Views: 1769

Answers (1)

Adil
Adil

Reputation: 231

By removing

.WithOpenApiOptions

methods, problem is fixed and it's showing parameter name and works with Swagger UI.

enter image description here

Upvotes: 6

Related Questions