Skip to content

Commit d5dae48

Browse files
committed
Apply the new Description in response models
1 parent bb3e442 commit d5dae48

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/OpenApi/src/Services/OpenApiDocumentService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,9 @@ private async Task<OpenApiResponse> GetResponseAsync(
356356
IOpenApiSchemaTransformer[] schemaTransformers,
357357
CancellationToken cancellationToken)
358358
{
359-
var description = ReasonPhrases.GetReasonPhrase(statusCode);
360359
var response = new OpenApiResponse
361360
{
362-
Description = description,
361+
Description = apiResponseType.Description ?? ReasonPhrases.GetReasonPhrase(statusCode),
363362
Content = new Dictionary<string, OpenApiMediaType>()
364363
};
365364

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.Responses.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,69 @@ await VerifyOpenApiDocument(builder, document =>
304304
});
305305
});
306306
}
307+
308+
[Fact]
309+
public async Task GetOpenApiResponse_UsesDescriptionSetByUser()
310+
{
311+
// Arrange
312+
var builder = CreateBuilder();
313+
314+
const string expectedCreatedDescription = "A new todo item was created";
315+
const string expectedBadRequestDescription = "Validation failed for the request";
316+
317+
// Act
318+
builder.MapGet("/api/todos",
319+
[ProducesResponseType(typeof(TimeSpan), StatusCodes.Status201Created, Description = expectedCreatedDescription)]
320+
[ProducesResponseType(StatusCodes.Status400BadRequest, Description = expectedBadRequestDescription)]
321+
() =>
322+
{ });
323+
324+
// Assert
325+
await VerifyOpenApiDocument(builder, document =>
326+
{
327+
var operation = Assert.Single(document.Paths["/api/todos"].Operations.Values);
328+
Assert.Collection(operation.Responses.OrderBy(r => r.Key),
329+
response =>
330+
{
331+
Assert.Equal("201", response.Key);
332+
Assert.Equal(expectedCreatedDescription, response.Value.Description);
333+
},
334+
response =>
335+
{
336+
Assert.Equal("400", response.Key);
337+
Assert.Equal(expectedBadRequestDescription, response.Value.Description);
338+
});
339+
});
340+
}
341+
342+
[Fact]
343+
public async Task GetOpenApiResponse_UsesStatusCodeReasonPhraseWhenExplicitDescriptionIsMissing()
344+
{
345+
// Arrange
346+
var builder = CreateBuilder();
347+
348+
// Act
349+
builder.MapGet("/api/todos",
350+
[ProducesResponseType(typeof(TimeSpan), StatusCodes.Status201Created, Description = null)] // Explicitly set to NULL
351+
[ProducesResponseType(StatusCodes.Status400BadRequest)] // Omitted, meaning it should be NULL
352+
() =>
353+
{ });
354+
355+
// Assert
356+
await VerifyOpenApiDocument(builder, document =>
357+
{
358+
var operation = Assert.Single(document.Paths["/api/todos"].Operations.Values);
359+
Assert.Collection(operation.Responses.OrderBy(r => r.Key),
360+
response =>
361+
{
362+
Assert.Equal("201", response.Key);
363+
Assert.Equal("Created", response.Value.Description);
364+
},
365+
response =>
366+
{
367+
Assert.Equal("400", response.Key);
368+
Assert.Equal("Bad Request", response.Value.Description);
369+
});
370+
});
371+
}
307372
}

0 commit comments

Comments
 (0)