From fc77e5ab963cbd10d5f1fae295ae98f61698acc0 Mon Sep 17 00:00:00 2001 From: moni-dips <126574585+moni-dips@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:59:25 +0100 Subject: [PATCH] Allow no match to be found to avoid throwing an exception (#3188) Allow no match to be found to avoid throwing an exception for raw content. --- .../SwaggerGenerator/SwaggerGenerator.cs | 2 +- .../SwaggerGenerator/SwaggerGeneratorTests.cs | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index d50ece3038..e18b0139d5 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -443,7 +443,7 @@ private async Task GenerateOpenApiOperationFromMetadataAsync(A } else { - bodyParameterDescription = apiDescription.ParameterDescriptions.Single(desc => desc.IsFromBody()); + bodyParameterDescription = apiDescription.ParameterDescriptions.SingleOrDefault(desc => desc.IsFromBody()); if (bodyParameterDescription is not null) { contentTypeValue.Schema = GenerateSchema( diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs index 15acdb9637..2aab436d56 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs @@ -2479,6 +2479,46 @@ public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithSt Assert.Equal(ParameterStyle.Form, content.Value.Encoding["param"].Style); } + [Fact] + public void GetSwagger_OpenApiOperationWithRawContent_IsHandled() + { + var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithParameter)); + var actionDescriptor = new ActionDescriptor + { + EndpointMetadata = new List() + { + new OpenApiOperation() + { + RequestBody = new OpenApiRequestBody() + { + Content = new Dictionary() + { + { "text/plain", new OpenApiMediaType() } + } + } + } + }, + RouteValues = new Dictionary + { + ["controller"] = methodInfo.DeclaringType.Name.Replace("Controller", string.Empty) + } + }; + var subject = Subject( + apiDescriptions: new[] + { + ApiDescriptionFactory.Create(actionDescriptor, methodInfo, groupName: "v1", httpMethod: "POST", relativePath: "resource"), + } + ); + + var document = subject.GetSwagger("v1"); + + Assert.Equal("V1", document.Info.Version); + Assert.Equal("Test API", document.Info.Title); + Assert.Equal(new[] { "/resource" }, document.Paths.Keys.ToArray()); + Assert.Equal(new[] { OperationType.Post }, document.Paths["/resource"].Operations.Keys); + Assert.Single(document.Paths["/resource"].Operations); + } + private static SwaggerGenerator Subject( IEnumerable apiDescriptions, SwaggerGeneratorOptions options = null,