11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Text . RegularExpressions ;
5+ using Microsoft . AspNetCore . Mvc ;
46using Microsoft . OpenApi . Models ;
57using 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}
0 commit comments