Skip to content

Commit 30ef0b5

Browse files
author
Olivier Leonard
committed
Extract getNullablePropertyType and getNullableSchemaType
1 parent cf24a1a commit 30ef0b5

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.regex.Pattern;
4545
import java.util.stream.Collectors;
4646

47-
import static org.openapitools.codegen.CodegenConstants.*;
4847
import static org.openapitools.codegen.languages.CSharpClientCodegen.GENERICHOST;
4948
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
5049
import static org.openapitools.codegen.utils.StringUtils.camelize;
@@ -1164,12 +1163,7 @@ protected void processOperation(CodegenOperation operation) {
11641163
}
11651164

11661165
String[] nestedTypes = {"List", "Collection", "ICollection", "Dictionary"};
1167-
String dataType = operation.returnProperty.items.dataType;
1168-
if (!GENERICHOST.equals(getLibrary())) {
1169-
if (operation.returnProperty.items.isNullable && (this.nullReferenceTypesFlag || operation.returnProperty.items.isEnum || getValueTypes().contains(dataType)) && !dataType.endsWith("?")) {
1170-
dataType += "?";
1171-
}
1172-
}
1166+
String dataType = getNullablePropertyType(operation.returnProperty.items);
11731167

11741168
for (String nestedType : nestedTypes) {
11751169
if (operation.returnType.contains("<" + nestedType + ">")) {
@@ -1442,18 +1436,21 @@ private String getArrayTypeDeclaration(Schema arr) {
14421436
String arrayType = typeMapping.get("array");
14431437
StringBuilder instantiationType = new StringBuilder(arrayType);
14441438
Schema<?> items = ModelUtils.getSchemaItems(arr);
1445-
String nestedType = getTypeDeclaration(items);
1439+
String nestedType = getNullableSchemaType(items);
14461440

1447-
if (!GENERICHOST.equals(getLibrary())) {
1448-
if (ModelUtils.isNullable(items) && (this.nullReferenceTypesFlag || ModelUtils.isEnumSchema(items) || getValueTypes().contains(nestedType)) && !nestedType.endsWith("?")) {
1449-
nestedType += "?";
1450-
}
1451-
}
14521441
// TODO: We may want to differentiate here between generics and primitive arrays.
14531442
instantiationType.append("<").append(nestedType).append(">");
14541443
return instantiationType.toString();
14551444
}
14561445

1446+
protected String getNullablePropertyType(CodegenProperty property) {
1447+
return property.dataType;
1448+
}
1449+
1450+
protected String getNullableSchemaType(Schema<?> items) {
1451+
return getTypeDeclaration(items);
1452+
}
1453+
14571454
@Override
14581455
public String toInstantiationType(Schema p) {
14591456
if (ModelUtils.isArraySchema(p)) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,4 +1704,24 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
17041704
generateYAMLSpecFile(objs);
17051705
return objs;
17061706
}
1707+
1708+
protected String getNullablePropertyType(CodegenProperty property) {
1709+
String dataType = super.getNullablePropertyType(property);
1710+
if (!GENERICHOST.equals(getLibrary())) {
1711+
if (property.isNullable && (this.nullReferenceTypesFlag || property.isEnum || getValueTypes().contains(dataType)) && !dataType.endsWith("?")) {
1712+
dataType += "?";
1713+
}
1714+
}
1715+
return dataType;
1716+
}
1717+
1718+
protected String getNullableSchemaType(Schema<?> items) {
1719+
String nestedType = super.getNullableSchemaType(items);
1720+
if (!GENERICHOST.equals(getLibrary())) {
1721+
if (ModelUtils.isNullable(items) && (this.nullReferenceTypesFlag || ModelUtils.isEnumSchema(items) || getValueTypes().contains(nestedType)) && !nestedType.endsWith("?")) {
1722+
nestedType += "?";
1723+
}
1724+
}
1725+
return nestedType;
1726+
}
17071727
}

0 commit comments

Comments
 (0)