From fac427dc4a959c2f3a23cb05bb3e4966741fbe7f Mon Sep 17 00:00:00 2001 From: devhl-labs Date: Sun, 11 Apr 2021 20:56:08 -0400 Subject: [PATCH 1/3] added nrt option --- .../codegen/CodegenConstants.java | 3 +++ .../languages/AbstractCSharpCodegen.java | 1 + .../languages/CSharpNetCoreClientCodegen.java | 22 ++++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index 2e7151dc94e2..4fb0299b5c56 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -208,6 +208,9 @@ public class CodegenConstants { public static final String DOTNET_FRAMEWORK = "targetFramework"; public static final String DOTNET_FRAMEWORK_DESC = "The target .NET framework version."; + public static final String NULLABLE_REFERENCE_TYPES = "nullableReferenceTypes"; + public static final String NULLABLE_REFERENCE_TYPES_DESC = "Set the nullable reference types property to true or false. Default is false."; + public static final String TEMPLATING_ENGINE = "templatingEngine"; public static final String TEMPLATING_ENGINE_DESC = "The templating engine plugin to use: \"mustache\" (default) or \"handlebars\" (beta)"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java index eb263b34c378..8a2bf57ba576 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java @@ -47,6 +47,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co protected boolean useCollection = false; protected boolean returnICollection = false; protected boolean netCoreProjectFileFlag = false; + protected boolean nullReferenceTypesFlag = false; protected String modelPropertyNaming = CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.PascalCase.name(); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java index ff90d1e30e26..8c50e5a7f540 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java @@ -230,6 +230,10 @@ public CSharpNetCoreClientCodegen() { cliOptions.add(modelPropertyNaming.defaultValue("PascalCase")); // CLI Switches + addSwitch(CodegenConstants.NULLABLE_REFERENCE_TYPES, + CodegenConstants.NULLABLE_REFERENCE_TYPES_DESC, + this.nullReferenceTypesFlag); + addSwitch(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC, this.hideGenerationTimestamp); @@ -555,9 +559,21 @@ public void processOpts() { * if (additionalProperties.containsKey(prop)) convertPropertyToBooleanAndWriteBack(prop); */ + if (additionalProperties.containsKey(CodegenConstants.NULLABLE_REFERENCE_TYPES)){ + Object nullableReferenceTypesFlag = additionalProperties.get(CodegenConstants.NULLABLE_REFERENCE_TYPES); + if (nullableReferenceTypesFlag == null || nullableReferenceTypesFlag.toString().trim().length() == 0 || nullableReferenceTypesFlag.toString().equals("true")){ + writePropertyBack(CodegenConstants.NULLABLE_REFERENCE_TYPES, true); + this.setNullableReferenceTypes(true); + this.nullableType.add("string"); + } + else{ + writePropertyBack(CodegenConstants.NULLABLE_REFERENCE_TYPES, false); + } + } + if (additionalProperties.containsKey(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT)) { this.setDisallowAdditionalPropertiesIfNotPresent(Boolean.parseBoolean(additionalProperties - .get(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT).toString())); + .get(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT).toString())); } if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES)) { @@ -738,6 +754,10 @@ public void processOpts() { additionalProperties.put("modelDocPath", modelDocPath); } + public void setNullableReferenceTypes(Boolean flag){ + this.nullReferenceTypesFlag = flag; + } + public void setNetStandard(Boolean netStandard) { this.netStandard = netStandard; } From 646685cef51f71c3dae56f4c2d2e43b69ea5b4d8 Mon Sep 17 00:00:00 2001 From: devhl-labs Date: Sun, 11 Apr 2021 21:13:03 -0400 Subject: [PATCH 2/3] build samples --- docs/generators/csharp-netcore.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/generators/csharp-netcore.md b/docs/generators/csharp-netcore.md index c7935752d018..840bcba517ee 100644 --- a/docs/generators/csharp-netcore.md +++ b/docs/generators/csharp-netcore.md @@ -17,6 +17,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |modelPropertyNaming|Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name| |PascalCase| |netCoreProjectFile|Use the new format (.NET Core) for .NET project files (.csproj).| |false| |nonPublicApi|Generates code with reduced access modifiers; allows embedding elsewhere without exposing non-public API calls to consumers.| |false| +|nullableReferenceTypes|Set the nullable reference types property to true or false. Default is false.| |false| |optionalAssemblyInfo|Generate AssemblyInfo.cs.| |true| |optionalEmitDefaultValues|Set DataMember's EmitDefaultValue.| |false| |optionalMethodArgument|C# Optional method argument, e.g. void square(int x=10) (.net 4.0+ only).| |true| From 0bad48d0880da606d9dfe99f68d417ef28c2e35c Mon Sep 17 00:00:00 2001 From: devhl-labs Date: Mon, 10 May 2021 20:35:26 -0400 Subject: [PATCH 3/3] moved switch to abstract --- .../codegen/languages/AbstractCSharpCodegen.java | 13 +++++++++++++ .../languages/CSharpNetCoreClientCodegen.java | 16 ---------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java index 8a2bf57ba576..2a53710a124e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java @@ -357,6 +357,12 @@ public void processOpts() { additionalProperties.put(CodegenConstants.NETCORE_PROJECT_FILE, netCoreProjectFileFlag); } + if (additionalProperties.containsKey(CodegenConstants.NULLABLE_REFERENCE_TYPES)) { + setNullableReferenceTypes(convertPropertyToBooleanAndWriteBack(CodegenConstants.NULLABLE_REFERENCE_TYPES)); + } else { + additionalProperties.put(CodegenConstants.NULLABLE_REFERENCE_TYPES, nullReferenceTypesFlag); + } + if (additionalProperties.containsKey(CodegenConstants.INTERFACE_PREFIX)) { String useInterfacePrefix = additionalProperties.get(CodegenConstants.INTERFACE_PREFIX).toString(); if ("false".equals(useInterfacePrefix.toLowerCase(Locale.ROOT))) { @@ -1116,6 +1122,13 @@ public void setInterfacePrefix(final String interfacePrefix) { this.interfacePrefix = interfacePrefix; } + public void setNullableReferenceTypes(final Boolean nullReferenceTypesFlag){ + this.nullReferenceTypesFlag = nullReferenceTypesFlag; + if (nullReferenceTypesFlag == true){ + this.nullableType.add("string"); + } + } + public void setEnumNameSuffix(final String enumNameSuffix) { this.enumNameSuffix = enumNameSuffix; } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java index 8c50e5a7f540..4167e054e2d1 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java @@ -559,18 +559,6 @@ public void processOpts() { * if (additionalProperties.containsKey(prop)) convertPropertyToBooleanAndWriteBack(prop); */ - if (additionalProperties.containsKey(CodegenConstants.NULLABLE_REFERENCE_TYPES)){ - Object nullableReferenceTypesFlag = additionalProperties.get(CodegenConstants.NULLABLE_REFERENCE_TYPES); - if (nullableReferenceTypesFlag == null || nullableReferenceTypesFlag.toString().trim().length() == 0 || nullableReferenceTypesFlag.toString().equals("true")){ - writePropertyBack(CodegenConstants.NULLABLE_REFERENCE_TYPES, true); - this.setNullableReferenceTypes(true); - this.nullableType.add("string"); - } - else{ - writePropertyBack(CodegenConstants.NULLABLE_REFERENCE_TYPES, false); - } - } - if (additionalProperties.containsKey(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT)) { this.setDisallowAdditionalPropertiesIfNotPresent(Boolean.parseBoolean(additionalProperties .get(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT).toString())); @@ -754,10 +742,6 @@ public void processOpts() { additionalProperties.put("modelDocPath", modelDocPath); } - public void setNullableReferenceTypes(Boolean flag){ - this.nullReferenceTypesFlag = flag; - } - public void setNetStandard(Boolean netStandard) { this.netStandard = netStandard; }