domaindrivendev / Swashbuckle.AspNetCore

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

Swagger generator fails to document API endpoint with multiple IFormFile parameters #2817

Open Moseyza opened 6 months ago

Moseyza commented 6 months ago

When defining an API endpoint in an ASP.NET Core Web API with multiple parameters of type IFormFile, the Swagger generator fails to generate the swagger.json file for the endpoint. The issue occurs when attempting to upload multiple files using IFormFile parameters in the API action.

Steps to Reproduce:

  1. Create an API endpoint with multiple parameters of type IFormFile.
  2. Ensure that Swagger middleware is configured and Swagger services are registered in the project.
  3. Attempt to generate the swagger.json file using Swashbuckle.
  4. Note that the Swagger generator fails to document the endpoint with multiple IFormFile parameters.

Code Snippet:


[HttpPost("dummy")]
public void DummyAction([FromForm] IFormFile o1, [FromForm] IFormFile o2)
{
    // Code for processing the uploaded files
}
martincostello commented 6 months ago

fails to generate the swagger.json file for the endpoint

fails to document the endpoint with multiple IFormFile parameters

Can you clarify which it is please?

Is it creating a swagger.json file with a missing operation, or is it failing to generate the document at all?

If the latter, what exceptions/errors are logged by the server, if any?

Moseyza commented 6 months ago

It throws this exception:

ArgumentException: An item with the same key has already been added. Key: ContentType

martincostello commented 6 months ago

Could you provide the full stack trace please? Then it should be much easier for us to find the source of the problem.

Moseyza commented 6 months ago

An unhandled exception occurred while processing the request. ArgumentException: An item with the same key has already been added. Key: ContentType System.Collections.Generic.Dictionary<TKey, TValue>.TryInsert(TKey key, TValue value, InsertionBehavior behavior)

SwaggerGeneratorException: Failed to generate Operation for action - TestApiForTestingSwashbuckle.Controllers.WeatherForecastController.DummyAction (TestApiForTestingSwashbuckle). See inner exception Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)

Stack: ArgumentException: An item with the same key has already been added. Key: ContentType System.Collections.Generic.Dictionary<TKey, TValue>.TryInsert(TKey key, TValue value, InsertionBehavior behavior) System.Collections.Generic.Dictionary<TKey, TValue>.Add(TKey key, TValue value) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateSchemaFromFormParameters(IEnumerable formParameters, SchemaRepository schemaRepository) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateRequestBodyFromFormParameters(ApiDescription apiDescription, SchemaRepository schemaRepository, IEnumerable formParameters) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateRequestBody(ApiDescription apiDescription, SchemaRepository schemaRepository) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)

[Uploading SampleProject.zip…]()

martincostello commented 6 months ago

Thanks. In future for GitHub issues, please provide details as text not screenshots. Text is much more useful for others as it can be searched, indexed, copied etc.

martincostello commented 6 months ago

Exception is coming from this line:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/a17b464184d4043c559e74e2c0d8596d7685cf63/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs#L538

martincostello commented 6 months ago

Turns out this is "expected" behaviour.

We might be able to improve this scenario anyway though so if it still fails it's at least more informative.

Moseyza commented 6 months ago

Thank you @martincostello. removing FromForm attributes obviates the exception and also files become uploaded without problem.