Skip to content

Bug Report: MapMethods with multiple HTTP methods fails #61363

@ls0001

Description

@ls0001

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Description
When using WebApplication.MapMethods() with multiple HTTP methods (e.g., both GET and POST), the OpenAPI generation fails with an exception

Like this:

  app.MapMethods("/weatherforecast", [HttpMethods.Get, HttpMethods.Post], TestHandler)
  .WithName("GetWeatherForecast")
  .WithOpenApi();   

Expected Behavior

The OpenAPI generator should properly handle endpoints with multiple HTTP methods ,
Properly handle collections of HTTP methods instead of expecting a single method

Steps To Reproduce

1.Create a Minimal API project
2.Create an endpoint with multiple HTTP methods:
In Programs.cs

public static void Main(string[] args){
  .........
  app.MapMethods("/weatherforecast", [HttpMethods.Get, HttpMethods.Post], TestHandler)
     .WithName("GetWeatherForecast")
     .WithOpenApi();
  app.Run();
}
public static IEnumerable<WeatherForecast> TestHandler()
{
   .........
}

3.Run the application
The following exception occurs:

Exceptions (if any)

Message:"Sequence contains more than one element":

At System.Linq.ThrowHelper.ThrowMoreThanOneElementException()
At System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable1 source, Boolean& found) At System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source)
At Microsoft.AspNetCore.OpenApi.OpenApiGenerator.GetOpenApiOperation(MethodInfo methodInfo, EndpointMetadataCollection metadata, RoutePattern pattern)
At Microsoft.AspNetCore.Builder.OpenApiEndpointConventionBuilderExtensions.AddAndConfigureOperationForEndpoint(EndpointBuilder endpointBuilder, Func`2 configure)

.NET Version

8.0.14

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions