Skip to content

Commit 94c583b

Browse files
A-Joshiwing328
authored andcommitted
[aspnetcore]: Preliminary support for ASP.NET 3.0 Core preview 5 (#2824)
* First set of fixes to support ver 3.0, useswashbuckle does not work yet * Fix swashbuckle issues iwth aspnetcore 3.0 * Use default routing for 2.2 and turn off default for 3.0 * fix up documentation * PR Feedback and wrong name in mustache file * Fix for 2.1 usage too * Change isFramework to useFrameworkReference as name to make thngs explicitly clear. Also fix small messages for the review comments * Make JSON.NET version configurable * Activate endpoint routing and use camel case NamingStrategy * Make Newtonsoft version configurable to match ASP.NET Core preview 5 * Fix spelling of an option, remove a duplicate call and update docs
1 parent fe8dd34 commit 94c583b

File tree

4 files changed

+131
-11
lines changed

4 files changed

+131
-11
lines changed

docs/generators/aspnetcore.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ sidebar_label: aspnetcore
1616
|packageVersion|C# package version.| |1.0.0|
1717
|packageGuid|The GUID that will be associated with the C# project| |null|
1818
|sourceFolder|source folder for generated code| |src|
19-
|compatibilityVersion|ASP.Net Core CompatibilityVersion| |Version_2_1|
20-
|aspnetCoreVersion|ASP.NET Core version: 2.2 (default), 2.1, 2.0 (deprecated)| |2.2|
19+
|compatibilityVersion|ASP.Net Core CompatibilityVersion| |Version_2_2|
20+
|aspnetCoreVersion|ASP.NET Core version: 3.0 (preview4 only), 2.2, 2.1, 2.0 (deprecated)| |2.2|
21+
|swashbuckleVersion|Swashbucke version: 3.0.0, 4.0.0| |3.0.0|
2122
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
2223
|useDateTimeOffset|Use DateTimeOffset to model date-time properties| |false|
2324
|useCollection|Deserialize array types to Collection<T> instead of List<T>.| |false|
2425
|returnICollection|Return ICollection<T> instead of the concrete type.| |false|
2526
|useSwashbuckle|Uses the Swashbuckle.AspNetCore NuGet package for documentation.| |true|
2627
|isLibrary|Is the build a library| |false|
28+
|useFrameworkReference|Use frameworkReference for ASP.NET Core 3.0+ and PackageReference ASP.NET Core 2.2 or earlier.| |false|
29+
|useNewtonsoft|Uses the Newtonsoft JSON library.| |true|
30+
|newtonsoftVersion|Version for Microsoft.AspNetCore.Mvc.NewtonsoftJson for ASP.NET Core 3.0+| |3.0.0-preview5-19227-01|
31+
|useDefaultRouting|Use default routing for the ASP.NET Core version. For 3.0 turn off default because it is not yet supported.| |true|
2732
|classModifier|Class Modifier can be empty, abstract| ||
2833
|operationModifier|Operation Modifier can be virtual, abstract or partial| |virtual|
2934
|buildTarget|Target to build an application or library| |program|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
3838

3939
public static final String USE_SWASHBUCKLE = "useSwashbuckle";
4040
public static final String ASPNET_CORE_VERSION = "aspnetCoreVersion";
41+
public static final String SWASHBUCKLE_VERSION = "swashbuckleVersion";
4142
public static final String CLASS_MODIFIER = "classModifier";
4243
public static final String OPERATION_MODIFIER = "operationModifier";
4344
public static final String OPERATION_IS_ASYNC = "operationIsAsync";
@@ -51,6 +52,10 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
5152
public static final String SDK_LIB = "Microsoft.NET.Sdk";
5253
public static final String COMPATIBILITY_VERSION = "compatibilityVersion";
5354
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";
5459

5560
private String packageGuid = "{" + randomUUID().toString().toUpperCase(Locale.ROOT) + "}";
5661

@@ -60,18 +65,23 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
6065
private boolean useSwashbuckle = true;
6166
protected int serverPort = 8080;
6267
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");
6469
; // 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)");
6571
private CliOption classModifier = new CliOption(CLASS_MODIFIER, "Class Modifier can be empty, abstract");
6672
private CliOption operationModifier = new CliOption(OPERATION_MODIFIER, "Operation Modifier can be virtual, abstract or partial");
6773
private CliOption modelClassModifier = new CliOption(MODEL_CLASS_MODIFIER, "Model Class Modifier can be nothing or partial");
6874
private boolean generateBody = true;
6975
private CliOption buildTarget = new CliOption("buildTarget", "Target to build an application or library");
7076
private String projectSdk = SDK_WEB;
71-
private String compatibilityVersion = "Version_2_1";
77+
private String compatibilityVersion = "Version_2_2";
7278
private boolean operationIsAsync = false;
7379
private boolean operationResultTask = false;
7480
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";
7585

7686
public AspNetCoreServerCodegen() {
7787
super();
@@ -146,10 +156,18 @@ public AspNetCoreServerCodegen() {
146156
aspnetCoreVersion.addEnum("2.0", "ASP.NET COre 2.0");
147157
aspnetCoreVersion.addEnum("2.1", "ASP.NET Core 2.1");
148158
aspnetCoreVersion.addEnum("2.2", "ASP.NET Core 2.2");
159+
aspnetCoreVersion.addEnum("3.0", "ASP.NET Core 3.0");
149160
aspnetCoreVersion.setDefault("2.2");
150161
aspnetCoreVersion.setOptValue(aspnetCoreVersion.getDefault());
151162
addOption(aspnetCoreVersion.getOpt(), aspnetCoreVersion.getDescription(), aspnetCoreVersion.getOptValue());
152163

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+
153171
// CLI Switches
154172
addSwitch(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG,
155173
CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC,
@@ -175,6 +193,23 @@ public AspNetCoreServerCodegen() {
175193
"Is the build a library",
176194
isLibrary);
177195

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+
178213
classModifier.addEnum("", "Keep class default with no modifier");
179214
classModifier.addEnum("abstract", "Make class abstract");
180215
classModifier.setDefault("");
@@ -246,6 +281,11 @@ public void processOpts() {
246281
}
247282
additionalProperties.put("packageGuid", packageGuid);
248283

284+
if (!additionalProperties.containsKey(NEWTONSOFT_VERSION)) {
285+
additionalProperties.put(NEWTONSOFT_VERSION, newtonsoftVersion);
286+
} else {
287+
newtonsoftVersion = (String)additionalProperties.get(NEWTONSOFT_VERSION);
288+
}
249289

250290
// CHeck for the modifiers etc.
251291
// The order of the checks is important.
@@ -275,6 +315,10 @@ public void processOpts() {
275315

276316
// determine the ASP.NET core version setting
277317
setAspnetCoreVersion(packageFolder);
318+
setSwashbuckleVersion();
319+
setIsFramework();
320+
setUseNewtonsoft();
321+
setUseEndpointRouting();
278322

279323
supportingFiles.add(new SupportingFile("build.sh.mustache", "", "build.sh"));
280324
supportingFiles.add(new SupportingFile("build.bat.mustache", "", "build.bat"));
@@ -479,10 +523,66 @@ private void setOperationIsAsync() {
479523
if (isLibrary) {
480524
operationIsAsync = false;
481525
additionalProperties.put(OPERATION_IS_ASYNC, operationIsAsync);
482-
} else if (additionalProperties.containsKey(OPERATION_IS_ASYNC)) {
526+
} else if (additionalProperties.containsKey(OPERATION_IS_ASYNC)) {
483527
operationIsAsync = convertPropertyToBooleanAndWriteBack(OPERATION_IS_ASYNC);
484528
} else {
485529
additionalProperties.put(OPERATION_IS_ASYNC, operationIsAsync);
486530
}
487531
}
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+
}
488588
}

modules/openapi-generator/src/main/resources/aspnetcore/2.1/Project.csproj.mustache

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@
1212
<PackageId>{{packageName}}</PackageId>
1313
</PropertyGroup>
1414
<ItemGroup>
15+
{{#useFrameworkReference}}
16+
{{#isLibrary}}
17+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
18+
{{/isLibrary}}
19+
{{/useFrameworkReference}}
20+
{{^useFrameworkReference}}
1521
<PackageReference Include="Microsoft.AspNetCore.App" />
22+
{{/useFrameworkReference}}
23+
{{#useNewtonsoft}}
24+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="{{newtonsoftVersion}}" />
25+
{{/useNewtonsoft}}
1626
{{#useSwashbuckle}}
17-
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0"/>
18-
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" />
27+
<PackageReference Include="Swashbuckle.AspNetCore" Version="{{swashbuckleVersion}}"/>
28+
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="{{swashbuckleVersion}}" />
1929
{{/useSwashbuckle}}
2030
</ItemGroup>
2131
<ItemGroup>

modules/openapi-generator/src/main/resources/aspnetcore/2.1/Startup.mustache

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ namespace {{packageName}}
6060

6161
// Add framework services.
6262
services
63-
.AddMvc()
63+
.AddMvc({{^useDefaultRoutng}}opts => opts.EnableEndpointRouting = false{{/useDefaultRoutng}})
6464
{{#compatibilityVersion}}
6565
.SetCompatibilityVersion(CompatibilityVersion.{{compatibilityVersion}})
6666
{{/compatibilityVersion}}
67-
.AddJsonOptions(opts =>
67+
.{{#useNewtonsoft}}AddNewtonsoftJson{{/useNewtonsoft}}{{^useNewtonsoft}}AddJsonOptions{{/useNewtonsoft}}(opts =>
6868
{
6969
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
7070
opts.SerializerSettings.Converters.Add(new StringEnumConverter
7171
{
72-
CamelCaseText = true
72+
{{#useNewtonsoft}}NamingStrategy = new CamelCaseNamingStrategy(){{/useNewtonsoft}}{{^useNewtonsoft}}CamelCaseText = true{{/useNewtonsoft}}
7373
});
7474
});
7575
{{#useSwashbuckle}}
@@ -127,7 +127,12 @@ namespace {{packageName}}
127127
128128
//TODO: Or alternatively use the original Swagger contract that's included in the static files
129129
// c.SwaggerEndpoint("/openapi-original.json", "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}} Original");
130-
}){{/useSwashbuckle}};
130+
}){{/useSwashbuckle}};{{^useDefaultRoutng}}
131+
app.UseRouting();
132+
app.UseEndpoints(endpoints =>
133+
{
134+
endpoints.MapControllers();
135+
});{{/useDefaultRoutng}}
131136
132137
if (env.IsDevelopment())
133138
{

0 commit comments

Comments
 (0)