11using System . Collections . Generic ;
22using System . Linq ;
33using Microsoft . OpenApi . Models ;
4- using Simplify . Web . Meta ;
54using Swashbuckle . AspNetCore . SwaggerGen ;
65
76namespace Simplify . Web . Swagger
@@ -11,14 +10,6 @@ namespace Simplify.Web.Swagger
1110 /// </summary>
1211 public class SimplifyWebDocumentFilter : IDocumentFilter
1312 {
14- // private const string VersionEndPoint = "/version";
15-
16- private static IList < string > RemovePrefixes = new List < string >
17- {
18- "Controllers." ,
19- "Api.v1."
20- } ;
21-
2213 /// <summary>
2314 /// Applies current filter
2415 /// </summary>
@@ -27,99 +18,36 @@ public class SimplifyWebDocumentFilter : IDocumentFilter
2718
2819 public void Apply ( OpenApiDocument openApiDocument , DocumentFilterContext context )
2920 {
30- foreach ( var item in CreatePathItemsFromControllersMetaData ( ) )
21+ foreach ( var item in ControllerActionsFactory . CreateControllerActionsFromControllersMetaData ( )
22+ . GroupBy ( x => x . Path )
23+ . Select ( x => new KeyValuePair < string , OpenApiPathItem > ( x . Key , CreatePathItem ( x ) ) ) )
3124 openApiDocument ? . Paths . Add ( item . Key , item . Value ) ;
3225 }
3326
34- private static IDictionary < string , OpenApiPathItem > CreatePathItemsFromControllersMetaData ( ) =>
35- ControllersMetaStore . Current . ControllersMetaData
36- . Where ( x => x . ExecParameters != null )
37- . SelectMany ( CreatePathItems )
38- . ToDictionary ( x => x . Key , x => x . Value ) ;
39-
40- private static IEnumerable < KeyValuePair < string , OpenApiPathItem > > CreatePathItems ( IControllerMetaData item )
41- {
42- var items = new List < KeyValuePair < string , OpenApiPathItem > > ( ) ;
43- var routes = item . ExecParameters ! . Routes ;
44- var needToAddPostfix = routes . ContainsDuplicates ( ) ;
45-
46- items . AddRange ( routes . Select ( x => new KeyValuePair < string , OpenApiPathItem > (
47- FormatControllerName ( x . Value , x . Key , needToAddPostfix ) , CreateOpenApiPathItems ( x . Key , x . Value , item ) ) ) ) ;
48-
49- return items ;
50- }
51-
52- private static OpenApiPathItem CreateOpenApiPathItems ( HttpMethod method , string route , IControllerMetaData item )
27+ private static OpenApiPathItem CreatePathItem ( IEnumerable < ControllerAction > actions )
5328 {
5429 var pathItem = new OpenApiPathItem ( ) ;
5530
56- pathItem . AddOperation ( HttpMethodToOperationType ( method ) , CreateOperation ( item ) ) ;
31+ foreach ( var item in actions )
32+ pathItem . AddOperation ( item . Type , CreateOperation ( item ) ) ;
5733
5834 return pathItem ;
5935 }
6036
61- private static OpenApiOperation CreateOperation ( IControllerMetaData item )
62- {
63- // TODO
6437
65- var operation = new OpenApiOperation
66- {
67- } ;
38+ private static OpenApiOperation CreateOperation ( ControllerAction item )
39+ {
40+ var operation = new OpenApiOperation ( ) ;
6841
6942 operation . Tags . Add ( new OpenApiTag
7043 {
71- Name = FormatOperationName ( item )
44+ Name = item . Names . GroupName
7245 } ) ;
7346
74- return operation ;
75- }
76-
77- private static OperationType HttpMethodToOperationType ( HttpMethod method ) =>
78- method switch
79- {
80- HttpMethod . Get => OperationType . Get ,
81- HttpMethod . Post => OperationType . Post ,
82- HttpMethod . Put => OperationType . Put ,
83- HttpMethod . Patch => OperationType . Patch ,
84- HttpMethod . Delete => OperationType . Delete ,
85- HttpMethod . Options => OperationType . Options ,
86- _ => OperationType . Get
87- } ;
47+ if ( item . Names . Summary != null )
48+ operation . Summary = item . Names . Summary ;
8849
89- // TODO
90- private static KeyValuePair < string , OpenApiResponse > CreateResponse ( )
91- {
92- var response = new OpenApiResponse
93- {
94- } ;
95-
96- return new KeyValuePair < string , OpenApiResponse > ( "" , response ) ;
97- }
98-
99- private static string FormatControllerName ( string route , HttpMethod method , bool needToAddPostfix ) =>
100- needToAddPostfix ? $ "{ route } ({ method } )" : route ;
101-
102- private static string ? FormatOperationName ( IControllerMetaData item ) =>
103- item . ControllerType . FullName != null ? FormatOperationName ( item . ControllerType . FullName ) : null ;
104-
105- private static string FormatOperationName ( string str )
106- {
107- foreach ( var prefix in RemovePrefixes )
108- {
109- var prefixIndex = str . IndexOf ( prefix ) ;
110-
111- if ( prefixIndex == - 1 )
112- continue ;
113-
114- str = str . Substring ( prefixIndex + prefix . Length ) ;
115- }
116-
117- str = str . Replace ( "." , "/" ) ;
118-
119- if ( str . EndsWith ( "Controller" ) )
120- str = str . Substring ( 0 , str . LastIndexOf ( "Controller" ) ) ;
121-
122- return str ;
50+ return operation ;
12351 }
12452 }
12553}
0 commit comments