Skip to content

Commit 4859d32

Browse files
committed
[#1] [add] basic responses with status code generation
1 parent 7e1254d commit 4859d32

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/Simplify.Web.Swagger/ControllerAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ControllerAction
2020
/// <summary>
2121
/// Controller responses
2222
/// </summary>
23-
public IList<OpenApiResponse> Responses = new List<OpenApiResponse>();
23+
public IDictionary<int, OpenApiResponse> Responses = new Dictionary<int, OpenApiResponse>();
2424

2525
/// <summary>
2626
/// Controller path

src/Simplify.Web.Swagger/ControllerActionsFactory.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text.RegularExpressions;
5+
using Microsoft.AspNetCore.Mvc;
46
using Microsoft.OpenApi.Models;
57
using Simplify.Web.Meta;
68

@@ -39,7 +41,8 @@ private static ControllerAction CreateControllerAction(HttpMethod method, string
3941
{
4042
Type = HttpMethodToOperationType(method),
4143
Path = route.StartsWith("/") ? route : "/" + route,
42-
Names = CreateNames(item.ControllerType)
44+
Names = CreateNames(item.ControllerType),
45+
Responses = CreateResponses(item.ControllerType)
4346
};
4447

4548
private static ControllerActionNames CreateNames(Type controllerType) =>
@@ -88,5 +91,55 @@ private static OperationType HttpMethodToOperationType(HttpMethod method) =>
8891
HttpMethod.Options => OperationType.Options,
8992
_ => OperationType.Get
9093
};
94+
95+
private static IDictionary<int, OpenApiResponse> CreateResponses(Type controllerType)
96+
{
97+
var items = new Dictionary<int, OpenApiResponse>();
98+
99+
var attributes = controllerType.GetCustomAttributes(typeof(ProducesResponseTypeAttribute), false);
100+
101+
foreach (ProducesResponseTypeAttribute item in attributes)
102+
items.Add(item.StatusCode, CreateResponse(item));
103+
104+
return items;
105+
}
106+
107+
private static OpenApiResponse CreateResponse(ProducesResponseTypeAttribute producesResponse)
108+
{
109+
var response = new OpenApiResponse();
110+
111+
response.Description = ResponseDescriptionMap
112+
.FirstOrDefault((entry) => Regex.IsMatch(producesResponse.StatusCode.ToString(), entry.Key))
113+
.Value;
114+
115+
return response;
116+
}
117+
118+
private static readonly IReadOnlyCollection<KeyValuePair<string, string>> ResponseDescriptionMap = new[]
119+
{
120+
new KeyValuePair<string, string>("1\\d{2}", "Information"),
121+
122+
new KeyValuePair<string, string>("201", "Created"),
123+
new KeyValuePair<string, string>("202", "Accepted"),
124+
new KeyValuePair<string, string>("204", "No Content"),
125+
new KeyValuePair<string, string>("2\\d{2}", "Success"),
126+
127+
new KeyValuePair<string, string>("304", "Not Modified"),
128+
new KeyValuePair<string, string>("3\\d{2}", "Redirect"),
129+
130+
new KeyValuePair<string, string>("400", "Bad Request"),
131+
new KeyValuePair<string, string>("401", "Unauthorized"),
132+
new KeyValuePair<string, string>("403", "Forbidden"),
133+
new KeyValuePair<string, string>("404", "Not Found"),
134+
new KeyValuePair<string, string>("405", "Method Not Allowed"),
135+
new KeyValuePair<string, string>("406", "Not Acceptable"),
136+
new KeyValuePair<string, string>("408", "Request Timeout"),
137+
new KeyValuePair<string, string>("409", "Conflict"),
138+
new KeyValuePair<string, string>("429", "Too Many Requests"),
139+
new KeyValuePair<string, string>("4\\d{2}", "Client Error"),
140+
141+
new KeyValuePair<string, string>("5\\d{2}", "Server Error"),
142+
new KeyValuePair<string, string>("default", "Error")
143+
};
91144
}
92145
}

src/Simplify.Web.Swagger/SimplifyWebDocumentFilter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ private static OpenApiOperation CreateOperation(ControllerAction item)
4646
if (item.Names.Summary != null)
4747
operation.Summary = item.Names.Summary;
4848

49+
foreach (var response in item.Responses)
50+
operation.Responses.Add(response.Key.ToString(), response.Value);
51+
4952
return operation;
5053
}
5154
}

0 commit comments

Comments
 (0)