From a08a082e4538bc1e71aa3acf7a18bf27a2429a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Garc=C3=ADa=20de=20la=20Noceda=20Arg=C3=BCelles?= Date: Mon, 30 Sep 2024 09:24:18 +0200 Subject: [PATCH] Fixing breaking change when applying filters for WithOpenApi extension --- .../SwaggerGenerator/SwaggerGenerator.cs | 2 +- ...on_For_WebApi_swaggerRequestUri=v1.verified.txt | 11 +++++++++++ ...tionTest.TypesAreRenderedCorrectly.verified.txt | 11 +++++++++++ test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs | 14 +++++++++++--- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index 730f01ecbb..f5207082eb 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -408,7 +408,7 @@ private async Task GenerateOpenApiOperationFromMetadataAsync(A { var (parameterAndContext, filterContext) = GenerateParameterAndContext(apiParameter, schemaRepository); parameter.Schema = parameterAndContext.Schema; - parameter.Description = parameterAndContext.Description; + parameter.Description ??= parameterAndContext.Description; foreach (var filter in _options.ParameterAsyncFilters) { diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_For_WebApi_swaggerRequestUri=v1.verified.txt b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_For_WebApi_swaggerRequestUri=v1.verified.txt index a024a3215d..84cdebb45c 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_For_WebApi_swaggerRequestUri=v1.verified.txt +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_For_WebApi_swaggerRequestUri=v1.verified.txt @@ -246,6 +246,17 @@ "tags": [ "WithOpenApi" ], + "parameters": [ + { + "name": "queryParameter", + "in": "query", + "description": "queryParameter Description", + "required": true, + "schema": { + "type": "string" + } + } + ], "requestBody": { "content": { "multipart/form-data": { diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.TypesAreRenderedCorrectly.verified.txt b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.TypesAreRenderedCorrectly.verified.txt index a024a3215d..84cdebb45c 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.TypesAreRenderedCorrectly.verified.txt +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.TypesAreRenderedCorrectly.verified.txt @@ -246,6 +246,17 @@ "tags": [ "WithOpenApi" ], + "parameters": [ + { + "name": "queryParameter", + "in": "query", + "description": "queryParameter Description", + "required": true, + "schema": { + "type": "string" + } + } + ], "requestBody": { "content": { "multipart/form-data": { diff --git a/test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs b/test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs index f8c22e9244..f446ffd754 100644 --- a/test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs +++ b/test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs @@ -42,10 +42,18 @@ public static IEndpointRouteBuilder MapWithOpenApiEndpoints(this IEndpointRouteB }) .WithOpenApi(); - group.MapPost("/IFromFile", (IFormFile file) => + group.MapPost("/IFromFile", (IFormFile file, string queryParameter) => { - return file.FileName; - }).WithOpenApi(); + return $"{file.FileName}{queryParameter}"; + }).WithOpenApi(o => + { + var parameter = o.Parameters.FirstOrDefault(p => p.Name.Equals("queryParameter", StringComparison.InvariantCulture)); + if (parameter is not null) + { + parameter.Description = $"{parameter.Name} Description"; + } + return o; + }); group.MapPost("/IFromFileCollection", (IFormFileCollection collection) => {