Skip to content

Commit ef31d31

Browse files
committed
Add more tests for regression
1 parent aee729a commit ef31d31

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,43 @@ await VerifyOpenApiDocument(builder, document =>
305305
});
306306
}
307307

308+
/// <remarks>
309+
/// Regression test for https://github.com/dotnet/aspnetcore/issues/60518
310+
/// </remarks>
311+
[Fact]
312+
public async Task GetOpenApiResponse_WithEmptyMethodBody_UsesDescriptionSetByUser()
313+
{
314+
// Arrange
315+
var builder = CreateBuilder();
316+
317+
const string expectedCreatedDescription = "A new todo item was created";
318+
const string expectedBadRequestDescription = "Validation failed for the request";
319+
320+
// Act
321+
builder.MapPost("/api/todos",
322+
[ProducesResponseType<Todo>(StatusCodes.Status200OK, Description = expectedCreatedDescription)]
323+
[ProducesResponseType(StatusCodes.Status400BadRequest)] // Omitted, meaning it should be NULL
324+
() =>
325+
{ });
326+
327+
// Assert
328+
await VerifyOpenApiDocument(builder, document =>
329+
{
330+
var operation = Assert.Single(document.Paths["/api/todos"].Operations.Values);
331+
Assert.Collection(operation.Responses.OrderBy(r => r.Key),
332+
response =>
333+
{
334+
Assert.Equal("200", response.Key);
335+
Assert.Equal(expectedCreatedDescription, response.Value.Description);
336+
},
337+
response =>
338+
{
339+
Assert.Equal("400", response.Key);
340+
Assert.Equal(expectedBadRequestDescription, response.Value.Description);
341+
});
342+
});
343+
}
344+
308345
[Fact]
309346
public async Task GetOpenApiResponse_UsesDescriptionSetByUser()
310347
{
@@ -369,4 +406,38 @@ await VerifyOpenApiDocument(builder, document =>
369406
});
370407
});
371408
}
409+
410+
/// <remarks>
411+
/// Regression test for https://github.com/dotnet/aspnetcore/issues/60518
412+
/// </remarks>
413+
[Fact]
414+
public async Task GetOpenApiResponse_WithEmptyMethodBody_UsesStatusCodeReasonPhraseWhenExplicitDescriptionIsMissing()
415+
{
416+
// Arrange
417+
var builder = CreateBuilder();
418+
419+
// Act
420+
builder.MapPost("/api/todos",
421+
[ProducesResponseType<Todo>(StatusCodes.Status200OK, Description = null)] // Explicitly set to NULL
422+
[ProducesResponseType(StatusCodes.Status400BadRequest)] // Omitted, meaning it should be NULL
423+
() =>
424+
{ });
425+
426+
// Assert
427+
await VerifyOpenApiDocument(builder, document =>
428+
{
429+
var operation = Assert.Single(document.Paths["/api/todos"].Operations.Values);
430+
Assert.Collection(operation.Responses.OrderBy(r => r.Key),
431+
response =>
432+
{
433+
Assert.Equal("200", response.Key);
434+
Assert.Equal("OK", response.Value.Description);
435+
},
436+
response =>
437+
{
438+
Assert.Equal("400", response.Key);
439+
Assert.Equal("Bad Request", response.Value.Description);
440+
});
441+
});
442+
}
372443
}

0 commit comments

Comments
 (0)