aspnetcore: Error "InvalidOperationException: Sequence contains more than one matching element" in Swagger when using multiple [FromForm] parameters in Minimal API
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
I have the following Minimal API endpoint:
app.MapPost("/api/people-minimalapi", ([FromForm] Person person, [FromForm] Address address)
=> TypedResults.NoContent())
.WithOpenApi();
public record class Person(string FirstName, string LastName);
public record class Address(string Street, string City, string State, string ZipCode);
When I launch the application, I get the following Exception related to Swagger:
InvalidOperationException: Sequence contains more than one matching element
System.Linq.ThrowHelper.ThrowMoreThanOneMatchException()
System.Linq.Enumerable.TryGetSingle<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate, out bool found)
System.Linq.Enumerable.SingleOrDefault<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOpenApiOperationFromMetadata(ApiDescription apiDescription, SchemaRepository schemaRepository)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerDocumentWithoutFilters(string documentName, string host, string basePath)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerAsync(string documentName, string host, string basePath)
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
If instead I use a Controller like this:
[ApiController]
[Route("api/[controller]")]
public class PeopleController : ControllerBase
{
[HttpPost]
public IActionResult Save([FromForm] Person person, [FromForm] Address address) => NoContent();
}
Everything works as expected.
Expected Behavior
Swagger should not throw the exception.
Steps To Reproduce
Minimal repro here: https://github.com/marcominerva/FromFormBindingIssue
Exceptions (if any)
InvalidOperationException: Sequence contains more than one matching element System.Linq.ThrowHelper.ThrowMoreThanOneMatchException() System.Linq.Enumerable.TryGetSingle<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate, out bool found) System.Linq.Enumerable.SingleOrDefault<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOpenApiOperationFromMetadata(ApiDescription apiDescription, SchemaRepository schemaRepository) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerDocumentWithoutFilters(string documentName, string host, string basePath) Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerAsync(string documentName, string host, string basePath) Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
.NET Version
8.0.101
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (8 by maintainers)
Hi all,
We just posted an announcement regarding our plans for OpenAPI support in ASP.NET Core moving forward in this post. We’re bringing OpenAPI document generation as a first class feature in ASP.NET Core.
@marcominerva I’ve included the scenario with multiple
FromFormparameters into our test bed and verified that the appropriate schema is generated for both Minimal APIs and controllers.@vdevc It looks like your issues is specifically related to the integration with Asp.Versioning. I’m eager to get this working with the built-in integration. Do you mind filing a separate issue so that we can track this work appropriately?
I’ve opened https://github.com/dotnet/aspnetcore/pull/55321 to resolve this issue in our built-in OpenAPI support.
The TL;DR is that we are now using
allOfto properly coalesce multiple parameters that are sources from the form body into the same request. I’ve also double checked that the correct schema is produced between MVC’s model binding semantics and minimal APIs model binding semantics.I edited the
test/Website/MinimalApi8/MinimalApi8.csprojinstead as it is exactly like in my scenario. You can see the commit in my fork here for your reference.The error occur on line 279 of SwaggerGenerator.cs
Member
apiDescription.ParameterDescriptionscontains 3 items one of which is duplicatedThe packages
comes from the excellent project from @commonsensesoftware available here.
Have you tested if DotSwashbuckle produces the same error? https://github.com/Havunen/DotSwashbuckle