From cad47fb41a22d82a9d12ac373ed6857a67d4a30b Mon Sep 17 00:00:00 2001 From: cremor Date: Mon, 13 May 2024 13:25:53 +0200 Subject: [PATCH] Add descriptions for more HTTP status codes (#2872) Add descriptions for more HTTP status codes. --- README.md | 8 ++--- .../SwaggerGenerator/SwaggerGenerator.cs | 35 ++++++++++++++++++- .../SwaggerGenerator/SwaggerGeneratorTests.cs | 14 ++++++-- .../wwwroot/swagger/v1/swagger.json | 4 +-- .../wwwroot/swagger/v1/swagger.json | 4 +-- test/WebSites/NswagClientExample/swagger.json | 4 +-- 6 files changed, 55 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 19b5dfe2f6..b3c5e4053c 100644 --- a/README.md +++ b/README.md @@ -394,7 +394,7 @@ Will produce the following response metadata: ``` responses: { 200: { - description: "Success", + description: "OK", content: { "application/json": { schema: { @@ -423,7 +423,7 @@ Will produce the following response metadata: ``` responses: { 200: { - description: "Success", + description: "OK", content: { "application/json": { schema: { @@ -446,7 +446,7 @@ responses: { } }, 500: { - description: "Server Error", + description: "Internal Server Error", content: {} } } @@ -788,7 +788,7 @@ If the generator encounters complex parameter or response types, it will generat ``` responses: { 200: { - description: "Success", + description: "OK", content: { "application/json": { schema: { diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index 8e0732a04c..24a37fcafd 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -667,31 +667,64 @@ private static bool IsFromFormAttributeUsedWithIFormFile(ApiParameterDescription private static readonly IReadOnlyCollection> ResponseDescriptionMap = new[] { - new KeyValuePair("1\\d{2}", "Information"), + new KeyValuePair("100", "Continue"), + new KeyValuePair("101", "Switching Protocols"), + new KeyValuePair("1\\d{2}", "Information"), + new KeyValuePair("200", "OK"), new KeyValuePair("201", "Created"), new KeyValuePair("202", "Accepted"), + new KeyValuePair("203", "Non-Authoritative Information"), new KeyValuePair("204", "No Content"), + new KeyValuePair("205", "Reset Content"), + new KeyValuePair("206", "Partial Content"), new KeyValuePair("2\\d{2}", "Success"), + new KeyValuePair("300", "Multiple Choices"), + new KeyValuePair("301", "Moved Permanently"), + new KeyValuePair("302", "Found"), + new KeyValuePair("303", "See Other"), new KeyValuePair("304", "Not Modified"), + new KeyValuePair("305", "Use Proxy"), + new KeyValuePair("307", "Temporary Redirect"), + new KeyValuePair("308", "Permanent Redirect"), new KeyValuePair("3\\d{2}", "Redirect"), new KeyValuePair("400", "Bad Request"), new KeyValuePair("401", "Unauthorized"), + new KeyValuePair("402", "Payment Required"), new KeyValuePair("403", "Forbidden"), new KeyValuePair("404", "Not Found"), new KeyValuePair("405", "Method Not Allowed"), new KeyValuePair("406", "Not Acceptable"), + new KeyValuePair("407", "Proxy Authentication Required"), new KeyValuePair("408", "Request Timeout"), new KeyValuePair("409", "Conflict"), + new KeyValuePair("410", "Gone"), + new KeyValuePair("411", "Length Required"), + new KeyValuePair("412", "Precondition Failed"), + new KeyValuePair("413", "Content Too Large"), + new KeyValuePair("414", "URI Too Long"), + new KeyValuePair("415", "Unsupported Media Type"), + new KeyValuePair("416", "Range Not Satisfiable"), + new KeyValuePair("417", "Expectation Failed"), + new KeyValuePair("421", "Misdirected Request"), + new KeyValuePair("422", "Unprocessable Content"), + new KeyValuePair("423", "Locked"), + new KeyValuePair("424", "Failed Dependency"), + new KeyValuePair("426", "Upgrade Required"), + new KeyValuePair("428", "Precondition Required"), new KeyValuePair("429", "Too Many Requests"), + new KeyValuePair("431", "Request Header Fields Too Large"), + new KeyValuePair("451", "Unavailable For Legal Reasons"), new KeyValuePair("4\\d{2}", "Client Error"), + new KeyValuePair("500", "Internal Server Error"), new KeyValuePair("501", "Not Implemented"), new KeyValuePair("502", "Bad Gateway"), new KeyValuePair("503", "Service Unavailable"), new KeyValuePair("504", "Gateway Timeout"), + new KeyValuePair("505", "HTTP Version Not Supported"), new KeyValuePair("5\\d{2}", "Server Error"), new KeyValuePair("default", "Error") diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs index 637f2589d8..185d649282 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text.Json; using System.Threading.Tasks; -using System.Reflection; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -943,6 +943,11 @@ public void GetSwagger_GeneratesResponses_ForSupportedResponseTypes() StatusCode = 400 }, new ApiResponseType + { + ApiResponseFormats = new [] { new ApiResponseFormat { MediaType = "application/json" } }, + StatusCode = 422 + }, + new ApiResponseType { ApiResponseFormats = new [] { new ApiResponseFormat { MediaType = "application/json" } }, IsDefaultResponse = true @@ -956,13 +961,16 @@ public void GetSwagger_GeneratesResponses_ForSupportedResponseTypes() var document = subject.GetSwagger("v1"); var operation = document.Paths["/resource"].Operations[OperationType.Post]; - Assert.Equal(new[] { "200", "400", "default" }, operation.Responses.Keys); + Assert.Equal(new[] { "200", "400", "422", "default" }, operation.Responses.Keys); var response200 = operation.Responses["200"]; - Assert.Equal("Success", response200.Description); + Assert.Equal("OK", response200.Description); Assert.Equal(new[] { "application/json" }, response200.Content.Keys); var response400 = operation.Responses["400"]; Assert.Equal("Bad Request", response400.Description); Assert.Empty(response400.Content.Keys); + var response422 = operation.Responses["422"]; + Assert.Equal("Unprocessable Content", response422.Description); + Assert.Empty(response422.Content.Keys); var responseDefault = operation.Responses["default"]; Assert.Equal("Error", responseDefault.Description); Assert.Empty(responseDefault.Content.Keys); diff --git a/test/WebSites/CliExample/wwwroot/swagger/v1/swagger.json b/test/WebSites/CliExample/wwwroot/swagger/v1/swagger.json index f62ce846cc..95aa261dd6 100644 --- a/test/WebSites/CliExample/wwwroot/swagger/v1/swagger.json +++ b/test/WebSites/CliExample/wwwroot/swagger/v1/swagger.json @@ -17,7 +17,7 @@ ], "responses": { "200": { - "description": "Success", + "description": "OK", "content": { "application/json": { "schema": { @@ -51,4 +51,4 @@ } } } -} \ No newline at end of file +} diff --git a/test/WebSites/CliExampleWithFactory/wwwroot/swagger/v1/swagger.json b/test/WebSites/CliExampleWithFactory/wwwroot/swagger/v1/swagger.json index 93f52c2296..eb21965c49 100644 --- a/test/WebSites/CliExampleWithFactory/wwwroot/swagger/v1/swagger.json +++ b/test/WebSites/CliExampleWithFactory/wwwroot/swagger/v1/swagger.json @@ -17,7 +17,7 @@ ], "responses": { "200": { - "description": "Success", + "description": "OK", "content": { "application/json": { "schema": { @@ -51,4 +51,4 @@ } } } -} \ No newline at end of file +} diff --git a/test/WebSites/NswagClientExample/swagger.json b/test/WebSites/NswagClientExample/swagger.json index d533a1b350..824dd5bc62 100644 --- a/test/WebSites/NswagClientExample/swagger.json +++ b/test/WebSites/NswagClientExample/swagger.json @@ -67,7 +67,7 @@ }, "responses": { "200": { - "description": "Success" + "description": "OK" } } } @@ -137,4 +137,4 @@ } } } -} \ No newline at end of file +}