Skip to content

Commit 79375b9

Browse files
Merge pull request #2797 from martincostello/gh-2740-add-missing-methods
Throw if unsupported HTTP method used
2 parents 8e744a1 + f47d1e6 commit 79375b9

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,15 @@ private IDictionary<OperationType, OpenApiOperation> GenerateOperations(
202202

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

205-
operations.Add(OperationTypeMap[httpMethod.ToUpper()], GenerateOperation(apiDescription, schemaRepository));
205+
var normalizedMethod = httpMethod.ToUpperInvariant();
206+
if (!OperationTypeMap.TryGetValue(normalizedMethod, out var operationType))
207+
{
208+
// See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2600 and
209+
// https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2740.
210+
throw new SwaggerGeneratorException($"The \"{httpMethod}\" HTTP method is not supported.");
211+
}
212+
213+
operations.Add(operationType, GenerateOperation(apiDescription, schemaRepository));
206214
};
207215

208216
return operations;

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text.Json;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Authentication;
7+
using Microsoft.AspNetCore.Http;
58
using Microsoft.AspNetCore.Mvc.Abstractions;
69
using Microsoft.AspNetCore.Mvc.ApiExplorer;
710
using Microsoft.AspNetCore.Mvc.ModelBinding;
8-
using Microsoft.AspNetCore.Http;
911
using Microsoft.AspNetCore.Routing;
1012
using Microsoft.OpenApi.Any;
1113
using Microsoft.OpenApi.Models;
1214
using Swashbuckle.AspNetCore.Swagger;
1315
using Swashbuckle.AspNetCore.TestSupport;
1416
using Xunit;
15-
using System.Threading.Tasks;
16-
using Microsoft.AspNetCore.Server.HttpSys;
17-
using Microsoft.AspNetCore.Authentication;
1817

1918
namespace Swashbuckle.AspNetCore.SwaggerGen.Test
2019
{
@@ -1409,6 +1408,31 @@ public void GetSwagger_SupportsOption_DocumentFilters()
14091408
Assert.Contains("ComplexType", document.Components.Schemas.Keys);
14101409
}
14111410

1411+
[Theory]
1412+
[InlineData("connect")]
1413+
[InlineData("CONNECT")]
1414+
[InlineData("FOO")]
1415+
public void GetSwagger_GeneratesSwaggerDocument_ThrowsIfHttpMethodNotSupported(string httpMethod)
1416+
{
1417+
var subject = Subject(
1418+
apiDescriptions: new[]
1419+
{
1420+
ApiDescriptionFactory.Create<FakeController>(
1421+
c => nameof(c.ActionWithNoParameters), groupName: "v1", httpMethod: httpMethod, relativePath: "resource"),
1422+
},
1423+
options: new SwaggerGeneratorOptions
1424+
{
1425+
SwaggerDocs = new Dictionary<string, OpenApiInfo>
1426+
{
1427+
["v1"] = new OpenApiInfo { Version = "V1", Title = "Test API" }
1428+
}
1429+
}
1430+
);
1431+
1432+
var exception = Assert.Throws<SwaggerGeneratorException>(() => subject.GetSwagger("v1"));
1433+
Assert.Equal($"The \"{httpMethod}\" HTTP method is not supported.", exception.Message);
1434+
}
1435+
14121436
private static SwaggerGenerator Subject(
14131437
IEnumerable<ApiDescription> apiDescriptions,
14141438
SwaggerGeneratorOptions options = null,

0 commit comments

Comments
 (0)