Skip to content

Commit e9bdab9

Browse files
Fix descriptions when using WithOpenApi (#3085)
Fixing breaking change when applying filters for WithOpenApi extension.
1 parent 17981d9 commit e9bdab9

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ private async Task<OpenApiOperation> GenerateOpenApiOperationFromMetadataAsync(A
408408
{
409409
var (parameterAndContext, filterContext) = GenerateParameterAndContext(apiParameter, schemaRepository);
410410
parameter.Schema = parameterAndContext.Schema;
411-
parameter.Description = parameterAndContext.Description;
411+
parameter.Description ??= parameterAndContext.Description;
412412

413413
foreach (var filter in _options.ParameterAsyncFilters)
414414
{

test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_For_WebApi_swaggerRequestUri=v1.verified.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@
246246
"tags": [
247247
"WithOpenApi"
248248
],
249+
"parameters": [
250+
{
251+
"name": "queryParameter",
252+
"in": "query",
253+
"description": "queryParameter Description",
254+
"required": true,
255+
"schema": {
256+
"type": "string"
257+
}
258+
}
259+
],
249260
"requestBody": {
250261
"content": {
251262
"multipart/form-data": {

test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.TypesAreRenderedCorrectly.verified.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@
246246
"tags": [
247247
"WithOpenApi"
248248
],
249+
"parameters": [
250+
{
251+
"name": "queryParameter",
252+
"in": "query",
253+
"description": "queryParameter Description",
254+
"required": true,
255+
"schema": {
256+
"type": "string"
257+
}
258+
}
259+
],
249260
"requestBody": {
250261
"content": {
251262
"multipart/form-data": {

test/WebSites/Basic/Controllers/FilesController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public IActionResult PostFiles(IFormFileCollection files)
2121
}
2222

2323
[HttpPost("form-with-file")]
24-
public IActionResult PostFormWithFile([FromForm]FormWithFile formWithFile)
24+
public IActionResult PostFormWithFile([FromForm] FormWithFile formWithFile)
2525
{
2626
throw new NotImplementedException();
2727
}
@@ -41,7 +41,7 @@ public FileResult GetFile(string name)
4141
writer.Flush();
4242
stream.Position = 0;
4343

44-
var contentType = name.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase) ? "application/zip" : "text/plain";
44+
var contentType = name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase) ? "application/zip" : "text/plain";
4545

4646
return File(stream, contentType, name);
4747
}

test/WebSites/WebApi/EndPoints/OpenApiEndpoints.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@ public static IEndpointRouteBuilder MapWithOpenApiEndpoints(this IEndpointRouteB
4242
})
4343
.WithOpenApi();
4444

45-
group.MapPost("/IFromFile", (IFormFile file) =>
45+
group.MapPost("/IFromFile", (IFormFile file, string queryParameter) =>
4646
{
47-
return file.FileName;
48-
}).WithOpenApi();
47+
return $"{file.FileName}{queryParameter}";
48+
}).WithOpenApi(o =>
49+
{
50+
var parameter = o.Parameters.FirstOrDefault(p => p.Name.Equals("queryParameter", StringComparison.OrdinalIgnoreCase));
51+
if (parameter is not null)
52+
{
53+
parameter.Description = $"{parameter.Name} Description";
54+
}
55+
return o;
56+
});
4957

5058
group.MapPost("/IFromFileCollection", (IFormFileCollection collection) =>
5159
{

0 commit comments

Comments
 (0)