Skip to content

Commit a3a37ce

Browse files
authored
Fix response code description in Minimal APIs (dotnet#45231)
* Fix response description dotnet#45192 * Test update dotnet#45192 * Use built-in response phrase dictionary dotnet#45192
1 parent fddab79 commit a3a37ce

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

src/OpenApi/src/OpenApiGenerator.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.AspNetCore.Mvc.Formatters;
1414
using Microsoft.AspNetCore.Routing;
1515
using Microsoft.AspNetCore.Routing.Patterns;
16+
using Microsoft.AspNetCore.WebUtilities;
1617
using Microsoft.Extensions.DependencyInjection;
1718
using Microsoft.Extensions.Hosting;
1819
using Microsoft.Extensions.Internal;
@@ -192,7 +193,7 @@ private static OpenApiResponses GetOpenApiResponses(MethodInfo method, EndpointM
192193

193194
foreach (var annotation in eligibileAnnotations)
194195
{
195-
var statusCode = annotation.Key.ToString(CultureInfo.InvariantCulture);
196+
var statusCode = annotation.Key;
196197

197198
// TODO: Use the discarded response Type for schema generation
198199
var (_, contentTypes) = annotation.Value;
@@ -203,7 +204,7 @@ private static OpenApiResponses GetOpenApiResponses(MethodInfo method, EndpointM
203204
responseContent[contentType] = new OpenApiMediaType();
204205
}
205206

206-
responses[statusCode] = new OpenApiResponse
207+
responses[statusCode.ToString(CultureInfo.InvariantCulture)] = new OpenApiResponse
207208
{
208209
Content = responseContent,
209210
Description = GetResponseDescription(statusCode)
@@ -212,23 +213,8 @@ private static OpenApiResponses GetOpenApiResponses(MethodInfo method, EndpointM
212213
return responses;
213214
}
214215

215-
private static string GetResponseDescription(string statusCode)
216-
{
217-
if (statusCode.Length != 3)
218-
{
219-
return string.Empty;
220-
}
221-
var first = statusCode[0];
222-
return first switch
223-
{
224-
'1' => "Information",
225-
'2' => "Success",
226-
'3' => "Redirection",
227-
'4' => "Client error",
228-
'5' => "Server error",
229-
_ => string.Empty,
230-
};
231-
}
216+
private static string GetResponseDescription(int statusCode)
217+
=> ReasonPhrases.GetReasonPhrase(statusCode);
232218

233219
private static void GenerateDefaultContent(MediaTypeCollection discoveredContentTypeAnnotation, Type? discoveredTypeAnnotation)
234220
{

src/OpenApi/test/OpenApiGeneratorTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ public void DefaultResponseDescriptionIsCorrect()
242242
Assert.Equal(2, operation.Responses.Count);
243243

244244
var successResponse = operation.Responses["201"];
245-
Assert.Equal("Success", successResponse.Description);
245+
Assert.Equal("Created", successResponse.Description);
246246

247247
var clientErrorResponse = operation.Responses["400"];
248-
Assert.Equal("Client error", clientErrorResponse.Description);
248+
Assert.Equal("Bad Request", clientErrorResponse.Description);
249249
}
250250

251251
[Fact]
@@ -259,10 +259,10 @@ public void DefaultResponseDescriptionIsCorrectForTwoSimilarResponses()
259259
Assert.Equal(2, operation.Responses.Count);
260260

261261
var continueResponse = operation.Responses["100"];
262-
Assert.Equal("Information", continueResponse.Description);
262+
Assert.Equal("Continue", continueResponse.Description);
263263

264264
var switchingProtocolsResponse = operation.Responses["101"];
265-
Assert.Equal("Information", switchingProtocolsResponse.Description);
265+
Assert.Equal("Switching Protocols", switchingProtocolsResponse.Description);
266266
}
267267

268268
[Fact]
@@ -279,19 +279,19 @@ public void AllDefaultResponseDescriptions()
279279
Assert.Equal(5, operation.Responses.Count);
280280

281281
var continueResponse = operation.Responses["100"];
282-
Assert.Equal("Information", continueResponse.Description);
282+
Assert.Equal("Continue", continueResponse.Description);
283283

284284
var createdResponse = operation.Responses["201"];
285-
Assert.Equal("Success", createdResponse.Description);
285+
Assert.Equal("Created", createdResponse.Description);
286286

287287
var multipleChoicesResponse = operation.Responses["300"];
288-
Assert.Equal("Redirection", multipleChoicesResponse.Description);
288+
Assert.Equal("Multiple Choices", multipleChoicesResponse.Description);
289289

290290
var badRequestResponse = operation.Responses["400"];
291-
Assert.Equal("Client error", badRequestResponse.Description);
291+
Assert.Equal("Bad Request", badRequestResponse.Description);
292292

293293
var InternalServerErrorResponse = operation.Responses["500"];
294-
Assert.Equal("Server error", InternalServerErrorResponse.Description);
294+
Assert.Equal("Internal Server Error", InternalServerErrorResponse.Description);
295295
}
296296

297297
[Fact]

0 commit comments

Comments
 (0)