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 2ee0c5474f8e..dbe2e61aa6f2 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 @@ -189,6 +189,9 @@ public class CodegenConstants { public static final String OPTIONAL_CONDITIONAL_SERIALIZATION = "conditionalSerialization"; public static final String OPTIONAL_CONDITIONAL_SERIALIZATION_DESC = "Serialize only those properties which are initialized by user, accepted values are true or false, default value is false."; + public static final String OPTIONAL_GENERATE_OPTIONAL_PROPERTIES_AS_NULLABLE = "optionalGenerateOptionalPropertiesAsNullable"; + public static final String OPTIONAL_GENERATE_OPTIONAL_PROPERTIES_AS_NULLABLE_DESC = "Generate optional properties as nullable."; + public static final String NETCORE_PROJECT_FILE = "netCoreProjectFile"; public static final String NETCORE_PROJECT_FILE_DESC = "Use the new format (.NET Core) for .NET project files (.csproj)."; 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 610322f0cf42..fb951cacd628 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 @@ -48,6 +48,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co protected boolean optionalAssemblyInfoFlag = true; protected boolean optionalEmitDefaultValuesFlag = false; + protected boolean optionalGenerateOptionalPropertiesAsNullableFlag = false; protected boolean conditionalSerialization = false; protected boolean optionalProjectFileFlag = true; protected boolean optionalMethodArgumentFlag = true; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java index 153d9b4b4710..e63e66899295 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java @@ -207,6 +207,10 @@ public CSharpClientCodegen() { addSwitch(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES, CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES_DESC, this.optionalEmitDefaultValuesFlag); + + addSwitch(CodegenConstants.OPTIONAL_GENERATE_OPTIONAL_PROPERTIES_AS_NULLABLE, + CodegenConstants.OPTIONAL_GENERATE_OPTIONAL_PROPERTIES_AS_NULLABLE_DESC, + this.optionalGenerateOptionalPropertiesAsNullableFlag); addSwitch(CodegenConstants.OPTIONAL_PROJECT_FILE, CodegenConstants.OPTIONAL_PROJECT_FILE_DESC, @@ -404,6 +408,12 @@ public void processOpts() { } else { additionalProperties.put(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES, optionalEmitDefaultValuesFlag); } + + if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_GENERATE_OPTIONAL_PROPERTIES_AS_NULLABLE)) { + setOptionalGenerateOptionalPropertiesAsNullableFlag(convertPropertyToBooleanAndWriteBack(CodegenConstants.OPTIONAL_GENERATE_OPTIONAL_PROPERTIES_AS_NULLABLE)); + } else { + additionalProperties.put(CodegenConstants.OPTIONAL_GENERATE_OPTIONAL_PROPERTIES_AS_NULLABLE, optionalGenerateOptionalPropertiesAsNullableFlag); + } if (additionalProperties.containsKey(CodegenConstants.NON_PUBLIC_API)) { setNonPublicApi(convertPropertyToBooleanAndWriteBack(CodegenConstants.NON_PUBLIC_API)); @@ -559,6 +569,10 @@ public void setOptionalAssemblyInfoFlag(boolean flag) { public void setOptionalEmitDefaultValuesFlag(boolean flag) { this.optionalEmitDefaultValuesFlag = flag; } + + public void setOptionalGenerateOptionalPropertiesAsNullableFlag(boolean flag) { + this.optionalGenerateOptionalPropertiesAsNullableFlag = flag; + } @Override public CodegenModel fromModel(String name, Schema model) { @@ -640,8 +654,10 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert postProcessPattern(property.pattern, property.vendorExtensions); postProcessEmitDefaultValue(property.vendorExtensions); super.postProcessModelProperty(model, property); - - if (!property.isContainer && (nullableType.contains(property.dataType) || property.isEnum)) { + + if(nullableType.contains(property.dataType)) { //optional + property.dataType = property.dataType + "?"; + } else if (!property.isContainer && (nullableType.contains(property.dataType) || property.isEnum)) { property.vendorExtensions.put("x-csharp-value-type", true); } }