Error "InvalidOperationException: Sequence contains more than one matching element" in Swagger when using multiple [FromForm] parameters in Minimal API (original) (raw)

Is there an existing issue for this?

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(IEnumerable source, Func<TSource, bool> predicate, out bool found)
System.Linq.Enumerable.SingleOrDefault(IEnumerable 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 apiDescriptions, SchemaRepository schemaRepository)
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable 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