Fix bugs (string comparison) in csharp generators (#15663)

* fix bugs in csharp generators

* undo change
This commit is contained in:
William Cheng 2023-05-29 15:17:54 +08:00 committed by GitHub
parent 784c700d37
commit 431cc2ec8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 46 deletions

View File

@ -472,7 +472,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
if (Boolean.TRUE.equals(this.zeroBasedEnums)) { if (Boolean.TRUE.equals(this.zeroBasedEnums)) {
property.vendorExtensions.put(this.zeroBasedEnumVendorExtension, true); property.vendorExtensions.put(this.zeroBasedEnumVendorExtension, true);
} else if (!Boolean.FALSE.equals(this.zeroBasedEnums)) { } else if (!Boolean.FALSE.equals(this.zeroBasedEnums)) {
if (property.allowableValues.containsKey("values")){ if (property.allowableValues.containsKey("values")) {
final List<Object> allowableValues = (List<Object>) property.allowableValues.get("values"); final List<Object> allowableValues = (List<Object>) property.allowableValues.get("values");
boolean isZeroBased = String.valueOf(allowableValues.get(0)).toLowerCase(Locale.ROOT).equals("unknown"); boolean isZeroBased = String.valueOf(allowableValues.get(0)).toLowerCase(Locale.ROOT).equals("unknown");
property.vendorExtensions.put(this.zeroBasedEnumVendorExtension, isZeroBased); property.vendorExtensions.put(this.zeroBasedEnumVendorExtension, isZeroBased);
@ -497,7 +497,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
if (Boolean.TRUE.equals(this.zeroBasedEnums)) { if (Boolean.TRUE.equals(this.zeroBasedEnums)) {
cm.vendorExtensions.put(this.zeroBasedEnumVendorExtension, true); cm.vendorExtensions.put(this.zeroBasedEnumVendorExtension, true);
} else if (!Boolean.FALSE.equals(this.zeroBasedEnums)) { } else if (!Boolean.FALSE.equals(this.zeroBasedEnums)) {
if (cm.allowableValues.containsKey("values")){ if (cm.allowableValues.containsKey("values")) {
final List<Object> allowableValues = (List<Object>) cm.allowableValues.get("values"); final List<Object> allowableValues = (List<Object>) cm.allowableValues.get("values");
boolean isZeroBased = String.valueOf(allowableValues.get(0)).toLowerCase(Locale.ROOT).equals("unknown"); boolean isZeroBased = String.valueOf(allowableValues.get(0)).toLowerCase(Locale.ROOT).equals("unknown");
cm.vendorExtensions.put(this.zeroBasedEnumVendorExtension, isZeroBased); cm.vendorExtensions.put(this.zeroBasedEnumVendorExtension, isZeroBased);

View File

@ -1399,35 +1399,35 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
properties.put(frameworkStrategy.name, strategies.stream().anyMatch(s -> s.name.equals(frameworkStrategy.name))); properties.put(frameworkStrategy.name, strategies.stream().anyMatch(s -> s.name.equals(frameworkStrategy.name)));
} }
if (strategies.stream().anyMatch(p -> Boolean.FALSE.equals(p.name == "netstandard1.3"))) { if (strategies.stream().anyMatch(p -> !"netstandard1.3".equals(p.name))) {
if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard1.4"))) { if (strategies.stream().anyMatch(p -> "netstandard1.4".equals(p.name))) {
properties.put(NET_STANDARD_14_OR_LATER, true); properties.put(NET_STANDARD_14_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard1.5"))) { } else if (strategies.stream().anyMatch(p -> "netstandard1.5".equals(p.name))) {
properties.put(NET_STANDARD_14_OR_LATER, true); properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true); properties.put(NET_STANDARD_15_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard1.6"))) { } else if (strategies.stream().anyMatch(p -> "netstandard1.6".equals(p.name))) {
properties.put(NET_STANDARD_14_OR_LATER, true); properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true); properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true); properties.put(NET_STANDARD_16_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard2.0"))) { } else if (strategies.stream().anyMatch(p -> "netstandard2.0".equals(p.name))) {
properties.put(NET_STANDARD_14_OR_LATER, true); properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true); properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true); properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true); properties.put(NET_STANDARD_20_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "netstandard2.1"))) { } else if (strategies.stream().anyMatch(p -> "netstandard2.1".equals(p.name))) {
properties.put(NET_STANDARD_14_OR_LATER, true); properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true); properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true); properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true); properties.put(NET_STANDARD_20_OR_LATER, true);
properties.put(NET_STANDARD_21_OR_LATER, true); properties.put(NET_STANDARD_21_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "net47"))) { } else if (strategies.stream().anyMatch(p -> "net47".equals(p.name))) {
properties.put(NET_STANDARD_14_OR_LATER, true); properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true); properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true); properties.put(NET_STANDARD_16_OR_LATER, true);
properties.put(NET_STANDARD_20_OR_LATER, true); properties.put(NET_STANDARD_20_OR_LATER, true);
properties.put(NET_STANDARD_21_OR_LATER, true); properties.put(NET_STANDARD_21_OR_LATER, true);
properties.put(NET_47_OR_LATER, true); properties.put(NET_47_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "net48"))) { } else if (strategies.stream().anyMatch(p -> "net48".equals(p.name))) {
properties.put(NET_STANDARD_14_OR_LATER, true); properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true); properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true); properties.put(NET_STANDARD_16_OR_LATER, true);
@ -1435,7 +1435,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
properties.put(NET_STANDARD_21_OR_LATER, true); properties.put(NET_STANDARD_21_OR_LATER, true);
properties.put(NET_47_OR_LATER, true); properties.put(NET_47_OR_LATER, true);
properties.put(NET_48_OR_LATER, true); properties.put(NET_48_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "net6.0"))) { } else if (strategies.stream().anyMatch(p -> "net6.0".equals(p.name))) {
properties.put(NET_STANDARD_14_OR_LATER, true); properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true); properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true); properties.put(NET_STANDARD_16_OR_LATER, true);
@ -1444,7 +1444,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
properties.put(NET_47_OR_LATER, true); properties.put(NET_47_OR_LATER, true);
properties.put(NET_48_OR_LATER, true); properties.put(NET_48_OR_LATER, true);
properties.put(NET_60_OR_LATER, true); properties.put(NET_60_OR_LATER, true);
} else if (strategies.stream().anyMatch(p -> Boolean.TRUE.equals(p.name == "net7.0"))){ } else if (strategies.stream().anyMatch(p -> "net7.0".equals(p.name))) {
properties.put(NET_STANDARD_14_OR_LATER, true); properties.put(NET_STANDARD_14_OR_LATER, true);
properties.put(NET_STANDARD_15_OR_LATER, true); properties.put(NET_STANDARD_15_OR_LATER, true);
properties.put(NET_STANDARD_16_OR_LATER, true); properties.put(NET_STANDARD_16_OR_LATER, true);
@ -1529,7 +1529,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
// some properties do not have isInherited set correctly // some properties do not have isInherited set correctly
// see modules\openapi-generator\src\test\resources\3_0\allOf.yaml // see modules\openapi-generator\src\test\resources\3_0\allOf.yaml
// Child properties Type, LastName, FirstName will have isInherited set to false when it should be true // Child properties Type, LastName, FirstName will have isInherited set to false when it should be true
if (cp.isInherited){ if (cp.isInherited) {
continue; continue;
} }
if (Boolean.TRUE.equals(cm.parentVars.stream().anyMatch(v -> v.baseName.equals(cp.baseName) && v.dataType.equals(cp.dataType)))) { if (Boolean.TRUE.equals(cm.parentVars.stream().anyMatch(v -> v.baseName.equals(cp.baseName) && v.dataType.equals(cp.dataType)))) {
@ -1551,15 +1551,15 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
private void ensureInheritedPropertiesArePresent(CodegenModel derivedModel) { private void ensureInheritedPropertiesArePresent(CodegenModel derivedModel) {
// every c# generator should definitely want this, or we should fix the issue // every c# generator should definitely want this, or we should fix the issue
// still, lets avoid breaking changes :( // still, lets avoid breaking changes :(
if (Boolean.FALSE.equals(GENERICHOST.equals(getLibrary()))){ if (Boolean.FALSE.equals(GENERICHOST.equals(getLibrary()))) {
return; return;
} }
if (derivedModel.parentModel == null){ if (derivedModel.parentModel == null) {
return; return;
} }
for (CodegenProperty parentProperty : derivedModel.parentModel.allVars){ for (CodegenProperty parentProperty : derivedModel.parentModel.allVars) {
if (Boolean.FALSE.equals(derivedModel.allVars.stream().anyMatch(v -> v.baseName.equals(parentProperty.baseName)))) { if (Boolean.FALSE.equals(derivedModel.allVars.stream().anyMatch(v -> v.baseName.equals(parentProperty.baseName)))) {
CodegenProperty clone = parentProperty.clone(); CodegenProperty clone = parentProperty.clone();
clone.isInherited = true; clone.isInherited = true;
@ -1582,7 +1582,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
objs = super.postProcessAllModels(objs); objs = super.postProcessAllModels(objs);
// other libraries probably want these fixes, but lets avoid breaking changes for now // other libraries probably want these fixes, but lets avoid breaking changes for now
if (Boolean.FALSE.equals(GENERICHOST.equals(getLibrary()))){ if (Boolean.FALSE.equals(GENERICHOST.equals(getLibrary()))) {
return objs; return objs;
} }
@ -1599,7 +1599,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
if (cm.getComposedSchemas() != null && cm.getComposedSchemas().getAllOf() != null && !cm.getComposedSchemas().getAllOf().isEmpty()) { if (cm.getComposedSchemas() != null && cm.getComposedSchemas().getAllOf() != null && !cm.getComposedSchemas().getAllOf().isEmpty()) {
cm.getComposedSchemas().getAllOf().forEach(allOf -> { cm.getComposedSchemas().getAllOf().forEach(allOf -> {
if (allOf.dataType.equals(cm.parent)){ if (allOf.dataType.equals(cm.parent)) {
allOf.isInherited = true; allOf.isInherited = true;
} }
}); });
@ -1607,28 +1607,28 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
ensureInheritedPropertiesArePresent(cm); ensureInheritedPropertiesArePresent(cm);
for (CodegenProperty property : cm.allVars){ for (CodegenProperty property : cm.allVars) {
fixInvalidPropertyName(cm, property); fixInvalidPropertyName(cm, property);
} }
for (CodegenProperty property : cm.vars){ for (CodegenProperty property : cm.vars) {
fixInvalidPropertyName(cm, property); fixInvalidPropertyName(cm, property);
} }
for (CodegenProperty property : cm.readWriteVars){ for (CodegenProperty property : cm.readWriteVars) {
fixInvalidPropertyName(cm, property); fixInvalidPropertyName(cm, property);
} }
for (CodegenProperty property : cm.optionalVars){ for (CodegenProperty property : cm.optionalVars) {
fixInvalidPropertyName(cm, property); fixInvalidPropertyName(cm, property);
} }
for (CodegenProperty property : cm.parentVars){ for (CodegenProperty property : cm.parentVars) {
fixInvalidPropertyName(cm, property); fixInvalidPropertyName(cm, property);
} }
for (CodegenProperty property : cm.requiredVars){ for (CodegenProperty property : cm.requiredVars) {
fixInvalidPropertyName(cm, property); fixInvalidPropertyName(cm, property);
} }
for (CodegenProperty property : cm.readOnlyVars){ for (CodegenProperty property : cm.readOnlyVars) {
fixInvalidPropertyName(cm, property); fixInvalidPropertyName(cm, property);
} }
for (CodegenProperty property : cm.nonNullableVars){ for (CodegenProperty property : cm.nonNullableVars) {
fixInvalidPropertyName(cm, property); fixInvalidPropertyName(cm, property);
} }
} }
@ -1636,7 +1636,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
return objs; return objs;
} }
private void fixInvalidPropertyName(CodegenModel model, CodegenProperty property){ private void fixInvalidPropertyName(CodegenModel model, CodegenProperty property) {
// TODO: remove once https://github.com/OpenAPITools/openapi-generator/pull/13681 is merged // TODO: remove once https://github.com/OpenAPITools/openapi-generator/pull/13681 is merged
if (property.name.equalsIgnoreCase(model.classname) || if (property.name.equalsIgnoreCase(model.classname) ||
reservedWords().contains(property.name) || reservedWords().contains(property.name) ||
@ -1654,7 +1654,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
*/ */
private void removePropertiesDeclaredInComposedClass(String className, List<CodegenModel> allModels, CodegenModel cm) { private void removePropertiesDeclaredInComposedClass(String className, List<CodegenModel> allModels, CodegenModel cm) {
CodegenModel otherModel = allModels.stream().filter(m -> m.classname.equals(className)).findFirst().orElse(null); CodegenModel otherModel = allModels.stream().filter(m -> m.classname.equals(className)).findFirst().orElse(null);
if (otherModel == null){ if (otherModel == null) {
return; return;
} }