8
8
import io .swagger .codegen .v3 .CodegenSecurity ;
9
9
import io .swagger .codegen .v3 .CodegenType ;
10
10
import io .swagger .codegen .v3 .SupportingFile ;
11
+ import io .swagger .codegen .v3 .utils .SemVer ;
11
12
import io .swagger .v3 .core .util .Json ;
12
13
import io .swagger .v3 .oas .models .OpenAPI ;
13
14
import io .swagger .v3 .oas .models .security .SecurityScheme ;
@@ -30,7 +31,9 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
30
31
private static final String ASP_NET_CORE_VERSION_OPTION = "--aspnet-core-version" ;
31
32
private static final String INTERFACE_ONLY_OPTION = "--interface-only" ;
32
33
private static final String INTERFACE_CONTROLLER_OPTION = "--interface-controller" ;
33
- private final String DEFAULT_ASP_NET_CORE_VERSION = "3.1" ;
34
+ private static final String SWASH_BUCKLE_VERSION_OPTION = "swashBuckleVersion" ;
35
+ private static final String TARGET_FRAMEWORK = "targetFramework" ;
36
+ private final String DEFAULT_ASP_NET_CORE_VERSION = "7.0" ;
34
37
private String aspNetCoreVersion ;
35
38
36
39
@ SuppressWarnings ("hiding" )
@@ -162,6 +165,7 @@ public void processOpts() {
162
165
supportingFiles .add (new SupportingFile ("Filters" + File .separator + "GeneratePathParamsValidationFilter.mustache" , packageFolder + File .separator + "Filters" , "GeneratePathParamsValidationFilter.cs" ));
163
166
supportingFiles .add (new SupportingFile ("Startup.mustache" , packageFolder , "Startup.cs" ));
164
167
} else {
168
+ final SemVer semVer = new SemVer (aspNetCoreVersion );
165
169
apiTemplateFiles .put ("3.0/controller.mustache" , ".cs" );
166
170
addInterfaceControllerTemplate ();
167
171
@@ -170,7 +174,20 @@ public void processOpts() {
170
174
171
175
supportingFiles .add (new SupportingFile ("3.0/Startup.mustache" , packageFolder , "Startup.cs" ));
172
176
supportingFiles .add (new SupportingFile ("3.0/Program.mustache" , packageFolder , "Program.cs" ));
173
- if (isThreeDotOneVersion ) {
177
+
178
+ if (semVer .atLeast ("5.0" )) {
179
+ additionalProperties .put (SWASH_BUCKLE_VERSION_OPTION , "6.4.0" );
180
+ supportingFiles .add (new SupportingFile ("3.1/Project.csproj.mustache" , packageFolder , this .packageName + ".csproj" ));
181
+ }
182
+ if (semVer .atLeast ("7.0" )) {
183
+ additionalProperties .put (TARGET_FRAMEWORK , "net7.0" );
184
+ } else if (semVer .atLeast ("6.0" )) {
185
+ additionalProperties .put (TARGET_FRAMEWORK , "net6.0" );
186
+ } else if (semVer .atLeast ("5.0" )) {
187
+ additionalProperties .put (TARGET_FRAMEWORK , "net5.0" );
188
+ } else if (semVer .atLeast ("3.1" )) {
189
+ additionalProperties .put (SWASH_BUCKLE_VERSION_OPTION , "5.5.1" );
190
+ additionalProperties .put (TARGET_FRAMEWORK , "netcoreapp3.1" );
174
191
supportingFiles .add (new SupportingFile ("3.1/Project.csproj.mustache" , packageFolder , this .packageName + ".csproj" ));
175
192
} else {
176
193
supportingFiles .add (new SupportingFile ("3.0/Project.csproj.mustache" , packageFolder , this .packageName + ".csproj" ));
@@ -284,7 +301,8 @@ protected void processOperation(CodegenOperation operation) {
284
301
List <CodegenContent > contents = operation .getContents ()
285
302
.stream ()
286
303
.filter (codegenContent -> !codegenContent .getIsForm ())
287
- .collect (Collectors .toList ());
304
+ .collect (
305
+ Collectors .toList ());
288
306
operation .getContents ().clear ();
289
307
operation .getContents ().addAll (contents );
290
308
}
@@ -345,7 +363,7 @@ private void addInterfaceControllerTemplate() {
345
363
boolean interfaceController = false ;
346
364
if (StringUtils .isNotBlank (interfaceControllerOption )) {
347
365
interfaceController = Boolean .valueOf (getOptionValue (INTERFACE_CONTROLLER_OPTION ));
348
- } else {
366
+ } else {
349
367
if (additionalProperties .get (INTERFACE_CONTROLLER_OPTION .substring (2 )) != null ) {
350
368
interfaceController = Boolean .valueOf (additionalProperties .get (INTERFACE_CONTROLLER_OPTION .substring (2 )).toString ());
351
369
}
@@ -378,7 +396,8 @@ private void setAspNetCoreVersion() {
378
396
} else {
379
397
this .aspNetCoreVersion = optionValue ;
380
398
}
381
- if (!this .aspNetCoreVersion .equals ("2.0" ) && !this .aspNetCoreVersion .equals ("2.1" ) && !this .aspNetCoreVersion .equals ("2.2" ) && !this .aspNetCoreVersion .equals ("3.0" )) {
399
+ final SemVer semVer = new SemVer (this .aspNetCoreVersion );
400
+ if (semVer .compareTo (new SemVer ("2.0" )) < 0 ) {
382
401
LOGGER .error ("version '" + this .aspNetCoreVersion + "' is not supported, switching to default version: '" + DEFAULT_ASP_NET_CORE_VERSION + "'" );
383
402
this .aspNetCoreVersion = DEFAULT_ASP_NET_CORE_VERSION ;
384
403
}
0 commit comments