Skip to content

Commit

Permalink
Add descriptions for more HTTP status codes (#2872)
Browse files Browse the repository at this point in the history
Add descriptions for more HTTP status codes.
  • Loading branch information
cremor committed May 13, 2024
1 parent ae3078f commit cad47fb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -394,7 +394,7 @@ Will produce the following response metadata:
```
responses: {
200: {
description: "Success",
description: "OK",
content: {
"application/json": {
schema: {
Expand Down Expand Up @@ -423,7 +423,7 @@ Will produce the following response metadata:
```
responses: {
200: {
description: "Success",
description: "OK",
content: {
"application/json": {
schema: {
Expand All @@ -446,7 +446,7 @@ responses: {
}
},
500: {
description: "Server Error",
description: "Internal Server Error",
content: {}
}
}
Expand Down Expand Up @@ -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: {
Expand Down
Expand Up @@ -667,31 +667,64 @@ private static bool IsFromFormAttributeUsedWithIFormFile(ApiParameterDescription

private static readonly IReadOnlyCollection<KeyValuePair<string, string>> ResponseDescriptionMap = new[]
{
new KeyValuePair<string, string>("1\\d{2}", "Information"),
new KeyValuePair<string, string>("100", "Continue"),
new KeyValuePair<string, string>("101", "Switching Protocols"),
new KeyValuePair<string, string>("1\\d{2}", "Information"),

new KeyValuePair<string, string>("200", "OK"),
new KeyValuePair<string, string>("201", "Created"),
new KeyValuePair<string, string>("202", "Accepted"),
new KeyValuePair<string, string>("203", "Non-Authoritative Information"),
new KeyValuePair<string, string>("204", "No Content"),
new KeyValuePair<string, string>("205", "Reset Content"),
new KeyValuePair<string, string>("206", "Partial Content"),
new KeyValuePair<string, string>("2\\d{2}", "Success"),

new KeyValuePair<string, string>("300", "Multiple Choices"),
new KeyValuePair<string, string>("301", "Moved Permanently"),
new KeyValuePair<string, string>("302", "Found"),
new KeyValuePair<string, string>("303", "See Other"),
new KeyValuePair<string, string>("304", "Not Modified"),
new KeyValuePair<string, string>("305", "Use Proxy"),
new KeyValuePair<string, string>("307", "Temporary Redirect"),
new KeyValuePair<string, string>("308", "Permanent Redirect"),
new KeyValuePair<string, string>("3\\d{2}", "Redirect"),

new KeyValuePair<string, string>("400", "Bad Request"),
new KeyValuePair<string, string>("401", "Unauthorized"),
new KeyValuePair<string, string>("402", "Payment Required"),
new KeyValuePair<string, string>("403", "Forbidden"),
new KeyValuePair<string, string>("404", "Not Found"),
new KeyValuePair<string, string>("405", "Method Not Allowed"),
new KeyValuePair<string, string>("406", "Not Acceptable"),
new KeyValuePair<string, string>("407", "Proxy Authentication Required"),
new KeyValuePair<string, string>("408", "Request Timeout"),
new KeyValuePair<string, string>("409", "Conflict"),
new KeyValuePair<string, string>("410", "Gone"),
new KeyValuePair<string, string>("411", "Length Required"),
new KeyValuePair<string, string>("412", "Precondition Failed"),
new KeyValuePair<string, string>("413", "Content Too Large"),
new KeyValuePair<string, string>("414", "URI Too Long"),
new KeyValuePair<string, string>("415", "Unsupported Media Type"),
new KeyValuePair<string, string>("416", "Range Not Satisfiable"),
new KeyValuePair<string, string>("417", "Expectation Failed"),
new KeyValuePair<string, string>("421", "Misdirected Request"),
new KeyValuePair<string, string>("422", "Unprocessable Content"),
new KeyValuePair<string, string>("423", "Locked"),
new KeyValuePair<string, string>("424", "Failed Dependency"),
new KeyValuePair<string, string>("426", "Upgrade Required"),
new KeyValuePair<string, string>("428", "Precondition Required"),
new KeyValuePair<string, string>("429", "Too Many Requests"),
new KeyValuePair<string, string>("431", "Request Header Fields Too Large"),
new KeyValuePair<string, string>("451", "Unavailable For Legal Reasons"),
new KeyValuePair<string, string>("4\\d{2}", "Client Error"),

new KeyValuePair<string, string>("500", "Internal Server Error"),
new KeyValuePair<string, string>("501", "Not Implemented"),
new KeyValuePair<string, string>("502", "Bad Gateway"),
new KeyValuePair<string, string>("503", "Service Unavailable"),
new KeyValuePair<string, string>("504", "Gateway Timeout"),
new KeyValuePair<string, string>("505", "HTTP Version Not Supported"),
new KeyValuePair<string, string>("5\\d{2}", "Server Error"),

new KeyValuePair<string, string>("default", "Error")
Expand Down
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/WebSites/CliExample/wwwroot/swagger/v1/swagger.json
Expand Up @@ -17,7 +17,7 @@
],
"responses": {
"200": {
"description": "Success",
"description": "OK",
"content": {
"application/json": {
"schema": {
Expand Down Expand Up @@ -51,4 +51,4 @@
}
}
}
}
}
Expand Up @@ -17,7 +17,7 @@
],
"responses": {
"200": {
"description": "Success",
"description": "OK",
"content": {
"application/json": {
"schema": {
Expand Down Expand Up @@ -51,4 +51,4 @@
}
}
}
}
}
4 changes: 2 additions & 2 deletions test/WebSites/NswagClientExample/swagger.json
Expand Up @@ -67,7 +67,7 @@
},
"responses": {
"200": {
"description": "Success"
"description": "OK"
}
}
}
Expand Down Expand Up @@ -137,4 +137,4 @@
}
}
}
}
}

0 comments on commit cad47fb

Please sign in to comment.