@@ -38,6 +38,7 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
38
38
39
39
public static final String USE_SWASHBUCKLE = "useSwashbuckle" ;
40
40
public static final String ASPNET_CORE_VERSION = "aspnetCoreVersion" ;
41
+ public static final String SWASHBUCKLE_VERSION = "swashbuckleVersion" ;
41
42
public static final String CLASS_MODIFIER = "classModifier" ;
42
43
public static final String OPERATION_MODIFIER = "operationModifier" ;
43
44
public static final String OPERATION_IS_ASYNC = "operationIsAsync" ;
@@ -51,6 +52,10 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
51
52
public static final String SDK_LIB = "Microsoft.NET.Sdk" ;
52
53
public static final String COMPATIBILITY_VERSION = "compatibilityVersion" ;
53
54
public static final String IS_LIBRARY = "isLibrary" ;
55
+ public static final String USE_FRAMEWORK_REFERENCE = "useFrameworkReference" ;
56
+ public static final String USE_NEWTONSOFT = "useNewtonsoft" ;
57
+ public static final String USE_DEFAULT_ROUTING = "useDefaultRouting" ;
58
+ public static final String NEWTONSOFT_VERSION = "newtonsoftVersion" ;
54
59
55
60
private String packageGuid = "{" + randomUUID ().toString ().toUpperCase (Locale .ROOT ) + "}" ;
56
61
@@ -60,18 +65,23 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
60
65
private boolean useSwashbuckle = true ;
61
66
protected int serverPort = 8080 ;
62
67
protected String serverHost = "0.0.0.0" ;
63
- protected CliOption aspnetCoreVersion = new CliOption (ASPNET_CORE_VERSION , "ASP.NET Core version: 2.2 (default), 2.1, 2.0 (deprecated) " );
68
+ protected CliOption swashbuckleVersion = new CliOption (SWASHBUCKLE_VERSION , "Swashbucke version: 3.0.0, 4.0.0 " );
64
69
; // default to 2.1
70
+ protected CliOption aspnetCoreVersion = new CliOption (ASPNET_CORE_VERSION , "ASP.NET Core version: 3.0 (preview4 only), 2.2, 2.1, 2.0 (deprecated)" );
65
71
private CliOption classModifier = new CliOption (CLASS_MODIFIER , "Class Modifier can be empty, abstract" );
66
72
private CliOption operationModifier = new CliOption (OPERATION_MODIFIER , "Operation Modifier can be virtual, abstract or partial" );
67
73
private CliOption modelClassModifier = new CliOption (MODEL_CLASS_MODIFIER , "Model Class Modifier can be nothing or partial" );
68
74
private boolean generateBody = true ;
69
75
private CliOption buildTarget = new CliOption ("buildTarget" , "Target to build an application or library" );
70
76
private String projectSdk = SDK_WEB ;
71
- private String compatibilityVersion = "Version_2_1 " ;
77
+ private String compatibilityVersion = "Version_2_2 " ;
72
78
private boolean operationIsAsync = false ;
73
79
private boolean operationResultTask = false ;
74
80
private boolean isLibrary = false ;
81
+ private boolean useFrameworkReference = false ;
82
+ private boolean useNewtonsoft = true ;
83
+ private boolean useDefaultRouting = true ;
84
+ private String newtonsoftVersion = "3.0.0-preview5-19227-01" ;
75
85
76
86
public AspNetCoreServerCodegen () {
77
87
super ();
@@ -146,10 +156,18 @@ public AspNetCoreServerCodegen() {
146
156
aspnetCoreVersion .addEnum ("2.0" , "ASP.NET COre 2.0" );
147
157
aspnetCoreVersion .addEnum ("2.1" , "ASP.NET Core 2.1" );
148
158
aspnetCoreVersion .addEnum ("2.2" , "ASP.NET Core 2.2" );
159
+ aspnetCoreVersion .addEnum ("3.0" , "ASP.NET Core 3.0" );
149
160
aspnetCoreVersion .setDefault ("2.2" );
150
161
aspnetCoreVersion .setOptValue (aspnetCoreVersion .getDefault ());
151
162
addOption (aspnetCoreVersion .getOpt (), aspnetCoreVersion .getDescription (), aspnetCoreVersion .getOptValue ());
152
163
164
+ swashbuckleVersion .addEnum ("3.0.0" , "Swashbuckle 3.0.0" );
165
+ swashbuckleVersion .addEnum ("4.0.0" , "Swashbuckle 4.0.0" );
166
+ swashbuckleVersion .addEnum ("5.0.0" , "Swashbuckle 5.0.0" );
167
+ swashbuckleVersion .setDefault ("3.0.0" );
168
+ swashbuckleVersion .setOptValue (swashbuckleVersion .getDefault ());
169
+ addOption (swashbuckleVersion .getOpt (), swashbuckleVersion .getDescription (), swashbuckleVersion .getOptValue ());
170
+
153
171
// CLI Switches
154
172
addSwitch (CodegenConstants .SORT_PARAMS_BY_REQUIRED_FLAG ,
155
173
CodegenConstants .SORT_PARAMS_BY_REQUIRED_FLAG_DESC ,
@@ -175,6 +193,23 @@ public AspNetCoreServerCodegen() {
175
193
"Is the build a library" ,
176
194
isLibrary );
177
195
196
+ addSwitch (USE_FRAMEWORK_REFERENCE ,
197
+ "Use frameworkReference for ASP.NET Core 3.0+ and PackageReference ASP.NET Core 2.2 or earlier." ,
198
+ useFrameworkReference );
199
+
200
+ addSwitch (USE_NEWTONSOFT ,
201
+ "Uses the Newtonsoft JSON library." ,
202
+ useNewtonsoft );
203
+
204
+ addOption (NEWTONSOFT_VERSION ,
205
+ "Version for Microsoft.AspNetCore.Mvc.NewtonsoftJson for ASP.NET Core 3.0+" ,
206
+ newtonsoftVersion );
207
+
208
+
209
+ addSwitch (USE_DEFAULT_ROUTING ,
210
+ "Use default routing for the ASP.NET Core version. For 3.0 turn off default because it is not yet supported." ,
211
+ useDefaultRouting );
212
+
178
213
classModifier .addEnum ("" , "Keep class default with no modifier" );
179
214
classModifier .addEnum ("abstract" , "Make class abstract" );
180
215
classModifier .setDefault ("" );
@@ -246,6 +281,11 @@ public void processOpts() {
246
281
}
247
282
additionalProperties .put ("packageGuid" , packageGuid );
248
283
284
+ if (!additionalProperties .containsKey (NEWTONSOFT_VERSION )) {
285
+ additionalProperties .put (NEWTONSOFT_VERSION , newtonsoftVersion );
286
+ } else {
287
+ newtonsoftVersion = (String )additionalProperties .get (NEWTONSOFT_VERSION );
288
+ }
249
289
250
290
// CHeck for the modifiers etc.
251
291
// The order of the checks is important.
@@ -275,6 +315,10 @@ public void processOpts() {
275
315
276
316
// determine the ASP.NET core version setting
277
317
setAspnetCoreVersion (packageFolder );
318
+ setSwashbuckleVersion ();
319
+ setIsFramework ();
320
+ setUseNewtonsoft ();
321
+ setUseEndpointRouting ();
278
322
279
323
supportingFiles .add (new SupportingFile ("build.sh.mustache" , "" , "build.sh" ));
280
324
supportingFiles .add (new SupportingFile ("build.bat.mustache" , "" , "build.bat" ));
@@ -479,10 +523,66 @@ private void setOperationIsAsync() {
479
523
if (isLibrary ) {
480
524
operationIsAsync = false ;
481
525
additionalProperties .put (OPERATION_IS_ASYNC , operationIsAsync );
482
- } else if (additionalProperties .containsKey (OPERATION_IS_ASYNC )) {
526
+ } else if (additionalProperties .containsKey (OPERATION_IS_ASYNC )) {
483
527
operationIsAsync = convertPropertyToBooleanAndWriteBack (OPERATION_IS_ASYNC );
484
528
} else {
485
529
additionalProperties .put (OPERATION_IS_ASYNC , operationIsAsync );
486
530
}
487
531
}
532
+
533
+ private void setIsFramework () {
534
+ if (aspnetCoreVersion .getOptValue ().startsWith ("3." )) {// default, do nothing
535
+ LOGGER .warn ("ASP.NET core version is " + aspnetCoreVersion .getOptValue () + " so changing to use frameworkReference instead of packageReference " );
536
+ useFrameworkReference = true ;
537
+ additionalProperties .put (USE_FRAMEWORK_REFERENCE , useFrameworkReference );
538
+ } else {
539
+ if (additionalProperties .containsKey (USE_FRAMEWORK_REFERENCE )) {
540
+ useFrameworkReference = convertPropertyToBooleanAndWriteBack (USE_FRAMEWORK_REFERENCE );
541
+ } else {
542
+ additionalProperties .put (USE_FRAMEWORK_REFERENCE , useFrameworkReference );
543
+ }
544
+ }
545
+
546
+ }
547
+
548
+ private void setUseNewtonsoft () {
549
+ if (aspnetCoreVersion .getOptValue ().startsWith ("2." )) {
550
+ LOGGER .warn ("ASP.NET core version is " + aspnetCoreVersion .getOptValue () + " so staying on default json library." );
551
+ useNewtonsoft = false ;
552
+ additionalProperties .put (USE_NEWTONSOFT , useNewtonsoft );
553
+ } else {
554
+ if (additionalProperties .containsKey (USE_NEWTONSOFT )) {
555
+ useNewtonsoft = convertPropertyToBooleanAndWriteBack (USE_NEWTONSOFT );
556
+ } else {
557
+ additionalProperties .put (USE_NEWTONSOFT , useNewtonsoft );
558
+ }
559
+ }
560
+ }
561
+
562
+ private void setUseEndpointRouting () {
563
+ if (aspnetCoreVersion .getOptValue ().startsWith ("3." )) {
564
+ LOGGER .warn ("ASP.NET core version is " + aspnetCoreVersion .getOptValue () + " so switching to old style endpoint routing." );
565
+ useDefaultRouting = false ;
566
+ additionalProperties .put (USE_DEFAULT_ROUTING , useDefaultRouting );
567
+ } else {
568
+ if (additionalProperties .containsKey (USE_DEFAULT_ROUTING )) {
569
+ useDefaultRouting = convertPropertyToBooleanAndWriteBack (USE_DEFAULT_ROUTING );
570
+ } else {
571
+ additionalProperties .put (USE_DEFAULT_ROUTING , useDefaultRouting );
572
+ }
573
+ }
574
+ }
575
+
576
+ private void setSwashbuckleVersion () {
577
+ setCliOption (swashbuckleVersion );
578
+
579
+ if (aspnetCoreVersion .getOptValue ().startsWith ("3." )) {
580
+ LOGGER .warn ("ASP.NET core version is " + aspnetCoreVersion .getOptValue () + " so changing default Swashbuckle version to 4.0.0." );
581
+ swashbuckleVersion .setOptValue ("4.0.0" );
582
+ additionalProperties .put (SWASHBUCKLE_VERSION , swashbuckleVersion .getOptValue ());
583
+ } else {
584
+ // default, do nothing
585
+ LOGGER .info ("Swashbuckle version: " + swashbuckleVersion .getOptValue ());
586
+ }
587
+ }
488
588
}
0 commit comments