33using System . Linq ;
44using System . Text . RegularExpressions ;
55using Microsoft . OpenApi . Models ;
6- using Simplify . Web . Meta ;
6+ using Simplify . Web . Controllers . Meta ;
7+ using Simplify . Web . Controllers . Meta . MetaStore ;
8+ using Simplify . Web . Controllers . Meta . Routing ;
9+ using Simplify . Web . Http ;
710using Swashbuckle . AspNetCore . SwaggerGen ;
811
912namespace Simplify . Web . Swagger
1013{
1114 /// <summary>
1215 /// Provides ControllerAction factory
1316 /// </summary>
14- public class ControllerActionsFactory
17+ public static class ControllerActionsFactory
1518 {
19+ private static readonly IReadOnlyCollection < KeyValuePair < string , string > > ResponseDescriptionMap =
20+ [
21+ new KeyValuePair < string , string > ( "1\\ d{2}" , "Information" ) ,
22+
23+ new KeyValuePair < string , string > ( "201" , "Created" ) ,
24+ new KeyValuePair < string , string > ( "202" , "Accepted" ) ,
25+ new KeyValuePair < string , string > ( "204" , "No Content" ) ,
26+ new KeyValuePair < string , string > ( "2\\ d{2}" , "Success" ) ,
27+
28+ new KeyValuePair < string , string > ( "304" , "Not Modified" ) ,
29+ new KeyValuePair < string , string > ( "3\\ d{2}" , "Redirect" ) ,
30+
31+ new KeyValuePair < string , string > ( "400" , "Bad Request" ) ,
32+ new KeyValuePair < string , string > ( "401" , "Unauthorized" ) ,
33+ new KeyValuePair < string , string > ( "403" , "Forbidden" ) ,
34+ new KeyValuePair < string , string > ( "404" , "Not Found" ) ,
35+ new KeyValuePair < string , string > ( "405" , "Method Not Allowed" ) ,
36+ new KeyValuePair < string , string > ( "406" , "Not Acceptable" ) ,
37+ new KeyValuePair < string , string > ( "408" , "Request Timeout" ) ,
38+ new KeyValuePair < string , string > ( "409" , "Conflict" ) ,
39+ new KeyValuePair < string , string > ( "429" , "Too Many Requests" ) ,
40+ new KeyValuePair < string , string > ( "4\\ d{2}" , "Client Error" ) ,
41+
42+ new KeyValuePair < string , string > ( "5\\ d{2}" , "Server Error" ) ,
43+ new KeyValuePair < string , string > ( "default" , "Error" )
44+ ] ;
45+
1646 /// <summary>
1747 /// Provides controller prefixes to remove
1848 /// </summary>
19- public static IList < string > RemovePrefixes = new List < string >
20- {
21- "Controllers." ,
22- "Api.v1."
23- } ;
49+ public static IList < string > RemovePrefixes { get ; } =
50+ [
51+ "Controllers." ,
52+ "Api.v1."
53+ ] ;
2454
2555 /// <summary>
2656 /// Creates controller actions from Simplify.Web controller meta data
2757 /// </summary>
2858 /// <returns></returns>
2959 public static IEnumerable < ControllerAction > CreateControllerActionsFromControllersMetaData ( DocumentFilterContext context ) =>
30- ControllersMetaStore . Current . ControllersMetaData
31- . Where ( x => x . ExecParameters != null )
60+ ControllersMetaStore . Current . RoutedControllers
3261 . SelectMany ( item => CreateControllerActions ( item , context ) ) ;
3362
34- private static IEnumerable < ControllerAction > CreateControllerActions ( IControllerMetaData item , DocumentFilterContext context ) =>
63+ private static IEnumerable < ControllerAction > CreateControllerActions ( IControllerMetadata item , DocumentFilterContext context ) =>
3564 item . ExecParameters !
3665 . Routes
3766 . Select ( x => CreateControllerAction ( x . Key , x . Value , item , context ) ) ;
3867
39- private static ControllerAction CreateControllerAction ( HttpMethod method , string route , IControllerMetaData item , DocumentFilterContext context ) =>
40- new ControllerAction
68+ private static ControllerAction CreateControllerAction ( HttpMethod method , IControllerRoute route , IControllerMetadata item , DocumentFilterContext context ) =>
69+ new ( )
4170 {
4271 Type = HttpMethodToOperationType ( method ) ,
43- Path = route . StartsWith ( "/" ) ? route : "/" + route ,
72+ ControllerRoute = route ,
4473 Names = CreateNames ( item . ControllerType ) ,
4574 Responses = CreateResponses ( item . ControllerType , context ) ,
4675 RequestBody = CreateRequestBody ( item . ControllerType , context ) ,
47- IsAuthorizationRequired = item . Security != null && item . Security . IsAuthorizationRequired
76+ IsAuthorizationRequired = item . Security is { IsAuthorizationRequired : true }
4877 } ;
4978
5079 private static ControllerActionNames CreateNames ( Type controllerType ) =>
@@ -56,10 +85,9 @@ private static ControllerActionNames CreateNames(string name)
5685
5786 var index = src . LastIndexOf ( "/" ) ;
5887
59- if ( index == - 1 )
60- return new ControllerActionNames ( src , src ) ;
61-
62- return new ControllerActionNames ( src , src . Substring ( 0 , index ) , src . Substring ( index + 1 ) ) ;
88+ return index == - 1
89+ ? new ControllerActionNames ( src , src )
90+ : new ControllerActionNames ( src , src . Substring ( 0 , index ) , src . Substring ( index + 1 ) ) ;
6391 }
6492
6593 private static string FormatNameSource ( string str )
@@ -99,72 +127,39 @@ private static OpenApiRequestBody CreateRequestBody(Type controllerType, Documen
99127 var request = new OpenApiRequestBody ( ) ;
100128 var attributes = controllerType . GetCustomAttributes ( typeof ( RequestBodyAttribute ) , false ) ;
101129
102- if ( attributes . Length > 0 )
103- {
104- var item = ( RequestBodyAttribute ) attributes . First ( ) ;
130+ if ( attributes . Length <= 0 )
131+ return request ;
105132
106- request . Content = new Dictionary < string , OpenApiMediaType >
107- {
108- [ "application/json" ] = new ( ) { Schema = context . SchemaGenerator . GenerateSchema ( item . Model , context . SchemaRepository ) }
109- } ;
110- }
133+ var item = ( RequestBodyAttribute ) attributes [ 0 ] ;
134+
135+ request . Content = new Dictionary < string , OpenApiMediaType >
136+ {
137+ [ "application/json" ] = new ( ) { Schema = context . SchemaGenerator . GenerateSchema ( item . Model , context . SchemaRepository ) }
138+ } ;
111139
112140 return request ;
113141 }
114142
115- private static IDictionary < int , OpenApiResponse > CreateResponses ( Type controllerType , DocumentFilterContext context )
116- {
117- var items = new Dictionary < int , OpenApiResponse > ( ) ;
118-
119- var attributes = controllerType . GetCustomAttributes ( typeof ( ProducesResponseAttribute ) , false ) ;
120-
121- foreach ( ProducesResponseAttribute item in attributes )
122- items . Add ( item . StatusCode , CreateResponse ( item , context ) ) ;
123-
124- return items ;
125- }
143+ private static IDictionary < int , OpenApiResponse > CreateResponses ( Type controllerType , DocumentFilterContext context ) =>
144+ controllerType . GetCustomAttributes ( typeof ( ProducesResponseAttribute ) , false )
145+ . Cast < ProducesResponseAttribute > ( )
146+ . ToDictionary ( item => item . StatusCode , item => CreateResponse ( item , context ) ) ;
126147
127148 private static OpenApiResponse CreateResponse ( ProducesResponseAttribute producesResponse , DocumentFilterContext context )
128149 {
129- var response = new OpenApiResponse ( ) ;
130-
131- response . Description = ResponseDescriptionMap
150+ var response = new OpenApiResponse
151+ {
152+ Description = ResponseDescriptionMap
132153 . FirstOrDefault ( ( entry ) => Regex . IsMatch ( producesResponse . StatusCode . ToString ( ) , entry . Key ) )
133- . Value ;
154+ . Value
155+ } ;
134156
135157 foreach ( var item in producesResponse . ContentTypes . Distinct ( ) )
136- response . Content . Add ( item , producesResponse . Type is null
137- ? new OpenApiMediaType ( )
138- : new ( ) { Schema = context . SchemaGenerator . GenerateSchema ( producesResponse . Type , context . SchemaRepository ) } ) ;
158+ response . Content . Add ( item , producesResponse . Type is null
159+ ? new OpenApiMediaType ( )
160+ : new OpenApiMediaType { Schema = context . SchemaGenerator . GenerateSchema ( producesResponse . Type , context . SchemaRepository ) } ) ;
139161
140162 return response ;
141163 }
142-
143- private static readonly IReadOnlyCollection < KeyValuePair < string , string > > ResponseDescriptionMap = new [ ]
144- {
145- new KeyValuePair < string , string > ( "1\\ d{2}" , "Information" ) ,
146-
147- new KeyValuePair < string , string > ( "201" , "Created" ) ,
148- new KeyValuePair < string , string > ( "202" , "Accepted" ) ,
149- new KeyValuePair < string , string > ( "204" , "No Content" ) ,
150- new KeyValuePair < string , string > ( "2\\ d{2}" , "Success" ) ,
151-
152- new KeyValuePair < string , string > ( "304" , "Not Modified" ) ,
153- new KeyValuePair < string , string > ( "3\\ d{2}" , "Redirect" ) ,
154-
155- new KeyValuePair < string , string > ( "400" , "Bad Request" ) ,
156- new KeyValuePair < string , string > ( "401" , "Unauthorized" ) ,
157- new KeyValuePair < string , string > ( "403" , "Forbidden" ) ,
158- new KeyValuePair < string , string > ( "404" , "Not Found" ) ,
159- new KeyValuePair < string , string > ( "405" , "Method Not Allowed" ) ,
160- new KeyValuePair < string , string > ( "406" , "Not Acceptable" ) ,
161- new KeyValuePair < string , string > ( "408" , "Request Timeout" ) ,
162- new KeyValuePair < string , string > ( "409" , "Conflict" ) ,
163- new KeyValuePair < string , string > ( "429" , "Too Many Requests" ) ,
164- new KeyValuePair < string , string > ( "4\\ d{2}" , "Client Error" ) ,
165-
166- new KeyValuePair < string , string > ( "5\\ d{2}" , "Server Error" ) ,
167- new KeyValuePair < string , string > ( "default" , "Error" )
168- } ;
169164 }
170165}
0 commit comments