Skip to content
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

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

Open
Moseyza opened this issue Apr 20, 2024 · 8 comments
Labels
help-wanted A change up for grabs for contributions from the community

Comments

@Moseyza
Copy link

Moseyza commented Apr 20, 2024

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
Copy link
Collaborator

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
Copy link
Author

Moseyza commented Apr 20, 2024

It throws this exception:

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

@martincostello
Copy link
Collaborator

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

@Moseyza
Copy link
Author

Moseyza commented Apr 21, 2024

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
Copy link
Collaborator

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
Copy link
Collaborator

Exception is coming from this line:

@martincostello martincostello added the help-wanted A change up for grabs for contributions from the community label Apr 22, 2024
@martincostello
Copy link
Collaborator

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
Copy link
Author

Moseyza commented Apr 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help-wanted A change up for grabs for contributions from the community
Projects
None yet
Development

No branches or pull requests

2 participants