Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional property for C# codegen to generate optional properties as nullable #14025

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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).";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down