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

Throw if unsupported HTTP method used #2797

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,17 @@ private OpenApiPaths GeneratePaths(IEnumerable<ApiDescription> apiDescriptions,

var apiDescription = (group.Count() > 1) ? _options.ConflictingActionsResolver(group) : group.Single();

operations.Add(OperationTypeMap[httpMethod.ToUpper()], GenerateOperation(apiDescription, schemaRepository));
var normalizedMethod = httpMethod.ToUpperInvariant();
if (!OperationTypeMap.TryGetValue(normalizedMethod, out var operationType))
{
// See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2600 and
// https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2740.
throw new SwaggerGeneratorException(string.Format(
"The \"{0}\" HTTP method is not supported.",
httpMethod));
martincostello marked this conversation as resolved.
Show resolved Hide resolved
}

operations.Add(operationType, GenerateOperation(apiDescription, schemaRepository));
};

return operations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.TestSupport;
using Xunit;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.AspNetCore.Authentication;

namespace Swashbuckle.AspNetCore.SwaggerGen.Test
{
Expand Down Expand Up @@ -1277,6 +1276,31 @@ public void GetSwagger_SupportsOption_DocumentFilters()
Assert.Contains("ComplexType", document.Components.Schemas.Keys);
}

[Theory]
[InlineData("connect")]
[InlineData("CONNECT")]
[InlineData("FOO")]
public void GetSwagger_GeneratesSwaggerDocument_ThrowsIfHttpMethodNotSupported(string httpMethod)
{
var subject = Subject(
apiDescriptions: new[]
{
ApiDescriptionFactory.Create<FakeController>(
c => nameof(c.ActionWithNoParameters), groupName: "v1", httpMethod: httpMethod, relativePath: "resource"),
},
options: new SwaggerGeneratorOptions
{
SwaggerDocs = new Dictionary<string, OpenApiInfo>
{
["v1"] = new OpenApiInfo { Version = "V1", Title = "Test API" }
}
}
);

var exception = Assert.Throws<SwaggerGeneratorException>(() => subject.GetSwagger("v1"));
Assert.Equal($"The \"{httpMethod}\" HTTP method is not supported.", exception.Message);
}

private static SwaggerGenerator Subject(
IEnumerable<ApiDescription> apiDescriptions,
SwaggerGeneratorOptions options = null,
Expand Down