33using System . Linq ;
44using Simplify . Web . Controllers . Meta . Routing ;
55using Swashbuckle . AspNetCore . SwaggerGen ;
6- #if NET10_0
76using Microsoft . OpenApi ;
87using JsonNode = System . Text . Json . Nodes . JsonNode ;
9- #else
10- using Microsoft . OpenApi . Models ;
11- #endif
128
139namespace Simplify . Web . Swagger ;
1410
@@ -55,19 +51,11 @@ var item in controllerActions
5551 PopulateDocumentTags ( swaggerDoc , controllerActions ) ;
5652 }
5753
58- #if NET10_0
5954 private static IList < IOpenApiParameter > CreateParameters ( ControllerAction item , DocumentFilterContext context ) =>
6055 item . ControllerRoute . Items
6156 . OfType < PathParameter > ( )
6257 . Select ( x => ( IOpenApiParameter ) CreatePathParameter ( x , item , context ) )
6358 . ToList ( ) ;
64- #else
65- private static IList < OpenApiParameter > CreateParameters ( ControllerAction item , DocumentFilterContext context ) =>
66- item . ControllerRoute . Items
67- . OfType < PathParameter > ( )
68- . Select ( x => CreatePathParameter ( x , item , context ) )
69- . ToList ( ) ;
70- #endif
7159
7260 private static OpenApiParameter CreatePathParameter ( PathParameter pathParam , ControllerAction item , DocumentFilterContext context )
7361 {
@@ -103,21 +91,15 @@ private OpenApiOperation CreateOperation(ControllerAction item, OpenApiDocument
10391 {
10492 var operation = new OpenApiOperation ( ) ;
10593
106- #if NET10_0
10794 operation . Tags ??= new HashSet < OpenApiTagReference > ( ) ;
10895 operation . Tags . Add ( new OpenApiTagReference ( item . Names . GroupName ) ) ;
109- #else
110- operation . Tags . Add ( new OpenApiTag { Name = item . Names . GroupName } ) ;
111- #endif
11296
11397 if ( item . Names . Summary != null )
11498 operation . Summary = item . Names . Summary ;
11599
116100 foreach ( var response in item . Responses )
117101 {
118- #if NET10_0
119102 operation . Responses ??= new OpenApiResponses ( ) ;
120- #endif
121103 operation . Responses . Add ( response . Key . ToString ( ) , response . Value ) ;
122104 }
123105
@@ -130,33 +112,13 @@ private OpenApiOperation CreateOperation(ControllerAction item, OpenApiDocument
130112 {
131113 var schemeNames = ResolveSecuritySchemeNames ( swaggerDoc ) ;
132114
133- #if NET10_0
134115 if ( schemeNames . Count > 0 )
135116 operation . Security = schemeNames
136117 . Select ( name => new OpenApiSecurityRequirement
137118 {
138119 [ new OpenApiSecuritySchemeReference ( name , swaggerDoc ) ] = [ ] ,
139120 } )
140121 . ToList ( ) ;
141- #else
142- if ( schemeNames . Count > 0 )
143- operation . Security = schemeNames
144- . Select ( name => new OpenApiSecurityRequirement
145- {
146- {
147- new OpenApiSecurityScheme
148- {
149- Reference = new OpenApiReference
150- {
151- Type = ReferenceType . SecurityScheme ,
152- Id = name ,
153- } ,
154- } ,
155- new List < string > ( )
156- } ,
157- } )
158- . ToList ( ) ;
159- #endif
160122 }
161123
162124 if ( _args == null )
@@ -190,21 +152,12 @@ private static OpenApiParameter CreateAcceptLanguageParameter(AcceptLanguageHead
190152 AllowEmptyValue = true ,
191153 } ;
192154
193- #if NET10_0
194155 param . Example = args . Default ;
195156 param . Schema = new OpenApiSchema
196157 {
197158 Default = args . Default ,
198159 Enum = args . Languages . Select ( l => ( JsonNode ) l ) . ToList ( )
199160 } ;
200- #else
201- param . Example = new Microsoft . OpenApi . Any . OpenApiString ( args . Default ) ;
202- param . Schema = new OpenApiSchema
203- {
204- Default = new Microsoft . OpenApi . Any . OpenApiString ( args . Default ) ,
205- Enum = args . Languages . Select ( l => ( Microsoft . OpenApi . Any . IOpenApiAny ) new Microsoft . OpenApi . Any . OpenApiString ( l ) ) . ToList ( )
206- } ;
207- #endif
208161
209162 return param ;
210163 }
@@ -214,12 +167,8 @@ private static void PopulateDocumentTags(
214167 IEnumerable < ControllerAction > actions
215168 )
216169 {
217- #if NET10_0
218170 var existingNames =
219171 swaggerDoc . Tags ? . Select ( t => t . Name ) . ToHashSet ( StringComparer . Ordinal ) ?? [ ] ;
220- #else
221- var existingNames = swaggerDoc . Tags . Select ( t => t . Name ) . ToHashSet ( StringComparer . Ordinal ) ;
222- #endif
223172
224173 foreach (
225174 var name in actions
@@ -228,9 +177,7 @@ var name in actions
228177 . Where ( n => ! existingNames . Contains ( n ) )
229178 )
230179 {
231- #if NET10_0
232180 swaggerDoc . Tags ??= new HashSet < OpenApiTag > ( ) ;
233- #endif
234181 swaggerDoc . Tags . Add ( new OpenApiTag { Name = name } ) ;
235182 }
236183 }
0 commit comments