Skip to content

Commit

Permalink
Merge pull request #2797 from martincostello/gh-2740-add-missing-methods
Browse files Browse the repository at this point in the history
Throw if unsupported HTTP method used
  • Loading branch information
domaindrivendev authored Apr 24, 2024
2 parents 8e744a1 + f47d1e6 commit 79375b9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,15 @@ private IDictionary<OperationType, OpenApiOperation> GenerateOperations(

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($"The \"{httpMethod}\" HTTP method is not supported.");
}

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 @@ -1409,6 +1408,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

0 comments on commit 79375b9

Please sign in to comment.