Skip to content

[OpenApi] Ignore unknown HTTP methods #63034

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

Merged
merged 2 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/OpenApi/sample/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;

[ApiController]
[Route("[controller]")]
Expand Down Expand Up @@ -39,6 +40,24 @@ public Ok<CurrentWeather> GetCurrentWeather()
return TypedResults.Ok(new CurrentWeather(1.0f));
}

[Route("/nohttpmethod")]
public ActionResult<CurrentWeather> NoHttpMethod()
=> Ok(new CurrentWeather(-100));

[HttpQuery]
[Route("/query")]
public ActionResult<CurrentWeather> HttpQueryMethod()
=> Ok(new CurrentWeather(0));

[HttpFoo] // See https://github.com/dotnet/aspnetcore/issues/60914
[Route("/unsupported")]
public ActionResult<CurrentWeather> UnsupportedHttpMethod()
=> Ok(new CurrentWeather(100));

public class HttpQuery() : HttpMethodAttribute(["QUERY"]);

public class HttpFoo() : HttpMethodAttribute(["FOO"]);

public class RouteParamsContainer
{
[FromRoute]
Expand Down
7 changes: 4 additions & 3 deletions src/OpenApi/src/Extensions/ApiDescriptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ internal static class ApiDescriptionExtensions
/// Maps the HTTP method of the ApiDescription to the HttpMethod.
/// </summary>
/// <param name="apiDescription">The ApiDescription to resolve an HttpMethod from.</param>
/// <returns>The <see cref="HttpMethod"/> associated with the given <paramref name="apiDescription"/>.</returns>
public static HttpMethod GetHttpMethod(this ApiDescription apiDescription) =>
/// <returns>The <see cref="HttpMethod"/> associated with the given <paramref name="apiDescription"/>, if known.</returns>
public static HttpMethod? GetHttpMethod(this ApiDescription apiDescription) =>
apiDescription.HttpMethod?.ToUpperInvariant() switch
{
"GET" => HttpMethod.Get,
Expand All @@ -28,7 +28,8 @@ public static HttpMethod GetHttpMethod(this ApiDescription apiDescription) =>
"HEAD" => HttpMethod.Head,
"OPTIONS" => HttpMethod.Options,
"TRACE" => HttpMethod.Trace,
_ => throw new InvalidOperationException($"Unsupported HTTP method: {apiDescription.HttpMethod}"),
"QUERY" => HttpMethod.Query,
_ => null,
};

/// <summary>
Expand Down
16 changes: 14 additions & 2 deletions src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,13 @@ private async Task<OpenApiPaths> GetOpenApiPathsAsync(
foreach (var descriptions in descriptionsByPath)
{
Debug.Assert(descriptions.Key != null, "Relative path mapped to OpenApiPath key cannot be null.");
paths.Add(descriptions.Key, new OpenApiPathItem { Operations = await GetOperationsAsync(descriptions, document, scopedServiceProvider, operationTransformers, schemaTransformers, cancellationToken) });
var operations = await GetOperationsAsync(descriptions, document, scopedServiceProvider, operationTransformers, schemaTransformers, cancellationToken);
if (operations.Count > 0)
{
paths.Add(descriptions.Key, new OpenApiPathItem { Operations = operations });
}
}

return paths;
}

Expand Down Expand Up @@ -280,7 +285,14 @@ private async Task<Dictionary<HttpMethod, OpenApiOperation>> GetOperationsAsync(
};

_operationTransformerContextCache.TryAdd(description.ActionDescriptor.Id, operationContext);
operations[description.GetHttpMethod()] = operation;

if (description.GetHttpMethod() is not { } method)
{
// Skip unsupported HTTP methods
continue;
}

operations[method] = operation;

// Use index-based for loop to avoid allocating an enumerator with a foreach.
for (var i = 0; i < operationTransformers.Length; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,4 @@ public void GetHttpMethod_ReturnsHttpMethodForApiDescription(string httpMethod,
// Assert
Assert.Equal(expectedHttpMethod, result);
}

[Theory]
[InlineData("UNKNOWN")]
[InlineData("unknown")]
public void GetHttpMethod_ThrowsForUnknownHttpMethod(string methodName)
{
// Arrange
var apiDescription = new ApiDescription
{
HttpMethod = methodName
};

// Act & Assert
var exception = Assert.Throws<InvalidOperationException>(() => apiDescription.GetHttpMethod());
Assert.Equal($"Unsupported HTTP method: {methodName}", exception.Message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@
}
}
}
},
"/query": {
"query": {
"tags": [
"Test"
],
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
}
}
}
}
}
}
},
"components": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@
}
}
}
},
"/query": {
"query": {
"tags": [
"Test"
],
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
}
}
}
}
}
}
},
"components": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,35 @@
}
}
}
},
"/query": {
"query": {
"tags": [
"Test"
],
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
}
}
}
}
}
}
},
"components": {
Expand Down
Loading