Skip to content

Commit 8843df6

Browse files
authored
[aspnetcore] - make more configurable and generate abstract class library #2181 (#2282)
* Add ability control out put generation and support to generate a library * Roll back pom version - it seems to break the CI/CD checks * Roll back pom version - it seems to break the CI/CD checks * Match with genrators - no changes from new code * Fix inadvertent changes * No idea why the names of params have changed pet to body for instance * Match generated document * Clarify logic as per PR review * Remove the generatewwwroot option and use the buildtarget option * Remove ar artifactVerson (not used), update docs and TODO notes * Add ability control out put generation and support to generate a library * Roll back pom version - it seems to break the CI/CD checks * Roll back pom version - it seems to break the CI/CD checks * Fix inadvertent changes * Match generated document * Add ability control out put generation and support to generate a library * Roll back pom version - it seems to break the CI/CD checks * Roll back pom version - it seems to break the CI/CD checks * Fix inadvertent changes * Match generated document * Clarify logic as per PR review * Remove the generatewwwroot option and use the buildtarget option * Remove ar artifactVerson (not used), update docs and TODO notes
1 parent 8c88f46 commit 8843df6

File tree

11 files changed

+198
-23
lines changed

11 files changed

+198
-23
lines changed

docs/generators/aspnetcore.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ sidebar_label: aspnetcore
77

88
| Option | Description | Values | Default |
99
| ------ | ----------- | ------ | ------- |
10+
|licenseUrl|The URL of the license| |http://localhost|
11+
|licenseName|The name of the license| |NoLicense|
12+
|packageCopyright|Specifies an AssemblyCopyright for the .NET Framework global assembly attributes stored in the AssemblyInfo file.| |No Copyright|
13+
|packageAuthors|Specifies Authors property in the .NET Core project file.| |OpenAPI|
14+
|packageTitle|Specifies an AssemblyTitle for the .NET Framework global assembly attributes stored in the AssemblyInfo file.| |OpenAPI Library|
1015
|packageName|C# package name (convention: Title.Case).| |Org.OpenAPITools|
1116
|packageVersion|C# package version.| |1.0.0|
1217
|packageGuid|The GUID that will be associated with the C# project| |null|
@@ -17,3 +22,7 @@ sidebar_label: aspnetcore
1722
|useCollection|Deserialize array types to Collection<T> instead of List<T>.| |false|
1823
|returnICollection|Return ICollection<T> instead of the concrete type.| |false|
1924
|useSwashbuckle|Uses the Swashbuckle.AspNetCore NuGet package for documentation.| |true|
25+
|classModifier|Class modifiers such as abstract or partial| ||
26+
|operationModifier|Operation modifiers such as virtual or abstract.| |virtual|
27+
|generateBody|Generates method body.| |true|
28+
|buildTarget|Target the build for a program or library.| |program|

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
4848

4949
protected String modelPropertyNaming = CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.PascalCase.name();
5050

51+
protected String licenseUrl = "http://localhost";
52+
protected String licenseName = "NoLicense";
53+
5154
protected String packageVersion = "1.0.0";
5255
protected String packageName = "Org.OpenAPITools";
5356
protected String packageTitle = "OpenAPI Library";
@@ -226,6 +229,7 @@ public void useDateTimeOffset(boolean flag) {
226229
}
227230
}
228231

232+
229233
@Override
230234
public void processOpts() {
231235
super.processOpts();
@@ -235,6 +239,19 @@ public void processOpts() {
235239
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
236240
}
237241

242+
// License info
243+
if (additionalProperties.containsKey(CodegenConstants.LICENSE_URL)) {
244+
setLicenseUrl((String) additionalProperties.get(CodegenConstants.LICENSE_URL));
245+
} else {
246+
additionalProperties.put(CodegenConstants.LICENSE_URL, this.licenseUrl);
247+
}
248+
249+
if (additionalProperties.containsKey(CodegenConstants.LICENSE_NAME)) {
250+
setLicenseName((String) additionalProperties.get(CodegenConstants.LICENSE_NAME));
251+
} else {
252+
additionalProperties.put(CodegenConstants.LICENSE_NAME, this.licenseName);
253+
}
254+
238255
// {{packageVersion}}
239256
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) {
240257
setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION));
@@ -927,6 +944,10 @@ public String toModelTestFilename(String name) {
927944
return toModelName(name) + "Tests";
928945
}
929946

947+
public void setLicenseUrl(String licenseUrl) {this.licenseUrl = licenseUrl;}
948+
949+
public void setLicenseName(String licenseName) {this.licenseName = licenseName;}
950+
930951
public void setPackageName(String packageName) {
931952
this.packageName = packageName;
932953
}

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

Lines changed: 134 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
3939

4040
public static final String USE_SWASHBUCKLE = "useSwashbuckle";
4141
public static final String ASPNET_CORE_VERSION = "aspnetCoreVersion";
42+
public static final String CLASS_MODIFIER = "classModifier";
43+
public static final String OPERATION_MODIFIER = "operationModifier";
44+
public static final String GENERATE_BODY = "generateBody";
45+
public static final String BUILD_TARGET = "buildTarget";
46+
47+
public static final String PROJECT_SDK = "projectSdk";
48+
public static final String SDK_WEB = "Microsoft.NET.Sdk.Web";
49+
public static final String SDK_LIB = "Microsoft.NET.Sdk";
4250

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

@@ -49,6 +57,12 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
4957
protected int serverPort = 8080;
5058
protected String serverHost = "0.0.0.0";
5159
protected String aspnetCoreVersion= "2.1"; // default to 2.1
60+
// TODO Make next two enums toensure fixed list.
61+
private String classModifier = "";
62+
private String operationModifier = "virtual";
63+
private boolean generateBody = true;
64+
private String buildTarget = "program";
65+
private String projectSdk = SDK_WEB;
5266

5367
public AspNetCoreServerCodegen() {
5468
super();
@@ -69,6 +83,26 @@ public AspNetCoreServerCodegen() {
6983
cliOptions.clear();
7084

7185
// CLI options
86+
addOption(CodegenConstants.LICENSE_URL,
87+
CodegenConstants.LICENSE_URL_DESC,
88+
licenseUrl);
89+
90+
addOption(CodegenConstants.LICENSE_NAME,
91+
CodegenConstants.LICENSE_NAME_DESC,
92+
licenseName);
93+
94+
addOption(CodegenConstants.PACKAGE_COPYRIGHT,
95+
CodegenConstants.PACKAGE_COPYRIGHT_DESC,
96+
packageCopyright);
97+
98+
addOption(CodegenConstants.PACKAGE_AUTHORS,
99+
CodegenConstants.PACKAGE_AUTHORS_DESC,
100+
packageAuthors);
101+
102+
addOption(CodegenConstants.PACKAGE_TITLE,
103+
CodegenConstants.PACKAGE_TITLE_DESC,
104+
packageTitle);
105+
72106
addOption(CodegenConstants.PACKAGE_NAME,
73107
"C# package name (convention: Title.Case).",
74108
packageName);
@@ -110,6 +144,22 @@ public AspNetCoreServerCodegen() {
110144
"Uses the Swashbuckle.AspNetCore NuGet package for documentation.",
111145
useSwashbuckle);
112146

147+
addOption(CLASS_MODIFIER,
148+
"Class modifiers such as abstract or partial",
149+
classModifier);
150+
151+
addOption(OPERATION_MODIFIER,
152+
"Operation modifiers such as virtual or abstract.",
153+
operationModifier);
154+
155+
addSwitch(GENERATE_BODY,
156+
"Generates method body.",
157+
generateBody);
158+
159+
addOption(BUILD_TARGET,
160+
"Target the build for a program or library.",
161+
buildTarget);
162+
113163
}
114164

115165
@Override
@@ -138,6 +188,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
138188
@Override
139189
public void processOpts() {
140190
super.processOpts();
191+
boolean isLibrary = false;
141192

142193
if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_GUID)) {
143194
setPackageGuid((String) additionalProperties.get(CodegenConstants.OPTIONAL_PROJECT_GUID));
@@ -155,6 +206,50 @@ public void processOpts() {
155206
setAspnetCoreVersion((String) additionalProperties.get(ASPNET_CORE_VERSION));
156207
}
157208

209+
// CHeck for class modifier if not present set the default value.
210+
if (additionalProperties.containsKey(CLASS_MODIFIER)) {
211+
classModifier = additionalProperties.get(CLASS_MODIFIER).toString();
212+
} else {
213+
additionalProperties.put(CLASS_MODIFIER, classModifier);
214+
}
215+
216+
// TODO Validate modifier values
217+
// If class modifierier is abstract then the methods need to be abstrat too.
218+
if ("abstract".equals(classModifier)) {
219+
operationModifier = classModifier;
220+
additionalProperties.put(OPERATION_MODIFIER, operationModifier);
221+
}
222+
223+
if (additionalProperties.containsKey(OPERATION_MODIFIER)) {
224+
operationModifier = additionalProperties.get(OPERATION_MODIFIER).toString();
225+
} else {
226+
additionalProperties.put(OPERATION_MODIFIER, operationModifier);
227+
}
228+
229+
// TODO Validate modifier values
230+
// If operation modifier is abstract then dont generate any body
231+
if ("abstract".equals(operationModifier)) {
232+
generateBody = false;
233+
additionalProperties.put(GENERATE_BODY, generateBody);
234+
}
235+
if (additionalProperties.containsKey(GENERATE_BODY)) {
236+
generateBody = convertPropertyToBooleanAndWriteBack(GENERATE_BODY);
237+
} else {
238+
additionalProperties.put(GENERATE_BODY, generateBody);
239+
}
240+
241+
// CHeck for class modifier if not present set the default value.
242+
if (additionalProperties.containsKey(BUILD_TARGET)) {
243+
buildTarget = additionalProperties.get(BUILD_TARGET).toString();
244+
} else {
245+
additionalProperties.put(BUILD_TARGET, buildTarget);
246+
}
247+
if ("library".equals(buildTarget)) {
248+
isLibrary = true;
249+
projectSdk = SDK_LIB;
250+
}
251+
additionalProperties.put(PROJECT_SDK, projectSdk);
252+
158253
additionalProperties.put("dockerTag", packageName.toLowerCase(Locale.ROOT));
159254

160255
apiPackage = packageName + ".Controllers";
@@ -173,35 +268,60 @@ public void processOpts() {
173268
throw new IllegalArgumentException("aspnetCoreVersion must be '2.1', '2.0' but found " + aspnetCoreVersion);
174269
}
175270

271+
176272
supportingFiles.add(new SupportingFile("build.sh.mustache", "", "build.sh"));
177273
supportingFiles.add(new SupportingFile("build.bat.mustache", "", "build.bat"));
178274
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
179275
supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
180-
supportingFiles.add(new SupportingFile("Dockerfile.mustache", packageFolder, "Dockerfile"));
181276
supportingFiles.add(new SupportingFile("gitignore", packageFolder, ".gitignore"));
182-
supportingFiles.add(new SupportingFile("appsettings.json", packageFolder, "appsettings.json"));
183-
184-
supportingFiles.add(new SupportingFile("Startup.mustache", packageFolder, "Startup.cs"));
185-
supportingFiles.add(new SupportingFile("Program.mustache", packageFolder, "Program.cs"));
186277
supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs"));
187278
supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj"));
279+
if (!isLibrary) {
280+
supportingFiles.add(new SupportingFile("Dockerfile.mustache", packageFolder, "Dockerfile"));
281+
supportingFiles.add(new SupportingFile("appsettings.json", packageFolder, "appsettings.json"));
282+
283+
supportingFiles.add(new SupportingFile("Startup.mustache", packageFolder, "Startup.cs"));
284+
supportingFiles.add(new SupportingFile("Program.mustache", packageFolder, "Program.cs"));
285+
supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json",
286+
packageFolder + File.separator + "Properties", "launchSettings.json"));
287+
} else {
288+
supportingFiles.add(new SupportingFile("Project.nuspec.mustache", packageFolder, packageName + ".nuspec"));
289+
// wwwroot files.
290+
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "README.md", packageFolder + File.separator + "wwwroot", "README.md"));
291+
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "index.html", packageFolder + File.separator + "wwwroot", "index.html"));
292+
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "web.config", packageFolder + File.separator + "wwwroot", "web.config"));
293+
294+
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "openapi-original.mustache",
295+
packageFolder + File.separator + "wwwroot", "openapi-original.json"));
296+
}
297+
supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs"));
298+
supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj"));
299+
if (!isLibrary) {
300+
supportingFiles.add(new SupportingFile("Dockerfile.mustache", packageFolder, "Dockerfile"));
301+
supportingFiles.add(new SupportingFile("appsettings.json", packageFolder, "appsettings.json"));
302+
303+
supportingFiles.add(new SupportingFile("Startup.mustache", packageFolder, "Startup.cs"));
304+
supportingFiles.add(new SupportingFile("Program.mustache", packageFolder, "Program.cs"));
305+
supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json",
306+
packageFolder + File.separator + "Properties", "launchSettings.json"));
307+
} else {
308+
supportingFiles.add(new SupportingFile("Project.nuspec.mustache", packageFolder, packageName + ".nuspec"));
309+
// wwwroot files.
310+
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "README.md", packageFolder + File.separator + "wwwroot", "README.md"));
311+
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "index.html", packageFolder + File.separator + "wwwroot", "index.html"));
312+
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "web.config", packageFolder + File.separator + "wwwroot", "web.config"));
313+
314+
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "openapi-original.mustache",
315+
packageFolder + File.separator + "wwwroot", "openapi-original.json"));
316+
}
188317

189-
supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json",
190-
packageFolder + File.separator + "Properties", "launchSettings.json"));
191318

192319
if (useSwashbuckle) {
193320
supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache",
194321
packageFolder + File.separator + "Filters", "BasePathFilter.cs"));
195322
supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache",
196323
packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs"));
197324
}
198-
199-
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "README.md", packageFolder + File.separator + "wwwroot", "README.md"));
200-
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "index.html", packageFolder + File.separator + "wwwroot", "index.html"));
201-
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "web.config", packageFolder + File.separator + "wwwroot", "web.config"));
202-
203-
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "openapi-original.mustache",
204-
packageFolder + File.separator + "wwwroot", "openapi-original.json"));
205325
}
206326

207327
public void setPackageGuid(String packageGuid) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="{{projectSdk}}">
22
<PropertyGroup>
33
<Description>{{packageName}}</Description>
44
<Copyright>{{packageName}}</Copyright>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<package >
3+
<metadata>
4+
<id>$id$</id>
5+
<version>{{packageVersion}}</version>
6+
<title>{{packageTitle}}</title>
7+
<authors>{{packageAuthors}}</authors>
8+
<owners>{{packageAuthors}}</owners>
9+
<licenseUrl>{{licenseUrl}}</licenseUrl>
10+
<!--
11+
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
12+
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
13+
-->
14+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
15+
<description>{{packageName}}</description>
16+
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
17+
<copyright>{{packageCopyright}}</copyright>
18+
<tags>{{packageName}}</tags>
19+
</metadata>
20+
</package>

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace {{packageName}}.Controllers
1515
/// {{description}}
1616
/// </summary>{{#description}}
1717
[Description("{{description}}")]{{/description}}
18-
public class {{classname}}Controller : ControllerBase
18+
public {{classModifier}} class {{classname}}Controller : ControllerBase
1919
{ {{#operation}}
2020
/// <summary>
2121
/// {{#summary}}{{summary}}{{/summary}}
@@ -28,7 +28,8 @@ namespace {{packageName}}.Controllers
2828
[ValidateModelState]{{#useSwashbuckle}}
2929
[SwaggerOperation("{{operationId}}")]{{#responses}}{{#dataType}}
3030
[SwaggerResponse(statusCode: {{code}}, type: typeof({{&dataType}}), description: "{{message}}")]{{/dataType}}{{^dataType}}{{/dataType}}{{/responses}}{{/useSwashbuckle}}
31-
public virtual IActionResult {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
31+
public {{operationModifier}} IActionResult {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
32+
{{#generateBody}}
3233
{ {{#responses}}
3334
{{#dataType}}
3435
//TODO: Uncomment the next line to return response {{code}} or use other options such as return this.NotFound(), return this.BadRequest(..), ...
@@ -49,6 +50,10 @@ namespace {{packageName}}.Controllers
4950
return new ObjectResult(example);{{/returnType}}{{^returnType}}
5051
throw new NotImplementedException();{{/returnType}}
5152
}
53+
{{/generateBody}}
54+
{{^generateBody}}
55+
;
56+
{{/generateBody}}
5257
{{/operation}}
5358
}
5459
{{/operations}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.0-SNAPSHOT
1+
4.0.0-SNAPSHOT

samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Org.OpenAPITools.Controllers
2323
/// <summary>
2424
///
2525
/// </summary>
26-
public class PetApiController : ControllerBase
26+
public class PetApiController : ControllerBase
2727
{
2828
/// <summary>
2929
/// Add a new pet to the store

samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Org.OpenAPITools.Controllers
2323
/// <summary>
2424
///
2525
/// </summary>
26-
public class StoreApiController : ControllerBase
26+
public class StoreApiController : ControllerBase
2727
{
2828
/// <summary>
2929
/// Delete purchase order by ID

samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Org.OpenAPITools.Controllers
2323
/// <summary>
2424
///
2525
/// </summary>
26-
public class UserApiController : ControllerBase
26+
public class UserApiController : ControllerBase
2727
{
2828
/// <summary>
2929
/// Create user

samples/server/petstore/aspnetcore/src/Org.OpenAPITools/wwwroot/openapi-original.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@
112112
"type" : "array",
113113
"items" : {
114114
"type" : "string",
115-
"default" : "available",
116-
"enum" : [ "available", "pending", "sold" ]
115+
"enum" : [ "available", "pending", "sold" ],
116+
"default" : "available"
117117
}
118118
}
119119
} ],

0 commit comments

Comments
 (0)