Adds fix and tests (#14102)

This commit is contained in:
Justin Black 2022-11-23 09:52:11 -05:00 committed by GitHub
parent dc1b2ed9e0
commit 3eb90a69e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 31 deletions

View File

@ -3033,17 +3033,17 @@ public class DefaultCodegen implements CodegenConfig {
if (schema.getAdditionalProperties() == null) {
if (!disallowAdditionalPropertiesIfNotPresent) {
isAdditionalPropertiesTrue = true;
addPropProp = fromProperty("", new Schema(), false);
addPropProp = fromProperty(getAdditionalPropertiesName(), new Schema(), false);
additionalPropertiesIsAnyType = true;
}
} else if (schema.getAdditionalProperties() instanceof Boolean) {
if (Boolean.TRUE.equals(schema.getAdditionalProperties())) {
isAdditionalPropertiesTrue = true;
addPropProp = fromProperty("", new Schema(), false);
addPropProp = fromProperty(getAdditionalPropertiesName(), new Schema(), false);
additionalPropertiesIsAnyType = true;
}
} else {
addPropProp = fromProperty("", (Schema) schema.getAdditionalProperties(), false);
addPropProp = fromProperty(getAdditionalPropertiesName(), (Schema) schema.getAdditionalProperties(), false);
if (ModelUtils.isAnyType((Schema) schema.getAdditionalProperties())) {
additionalPropertiesIsAnyType = true;
}
@ -3853,13 +3853,7 @@ public class DefaultCodegen implements CodegenConfig {
}
// handle inner property
String itemName = null;
if (p.getExtensions() != null && p.getExtensions().get("x-item-name") != null) {
itemName = p.getExtensions().get("x-item-name").toString();
}
if (itemName == null) {
itemName = property.name;
}
String itemName = getItemsName(p, name);
ArraySchema arraySchema = (ArraySchema) p;
Schema innerSchema = unaliasSchema(getSchemaItems(arraySchema));
CodegenProperty cp = fromProperty(itemName, innerSchema, false);
@ -7355,6 +7349,17 @@ public class DefaultCodegen implements CodegenConfig {
addRequiredVarsMap(schema, property);
}
protected String getItemsName(Schema containingSchema, String containingSchemaName) {
// fromProperty use case
if (containingSchema.getExtensions() != null && containingSchema.getExtensions().get("x-item-name") != null) {
return containingSchema.getExtensions().get("x-item-name").toString();
}
return toVarName(containingSchemaName);
}
protected String getAdditionalPropertiesName() {
return "additional_properties";
}
private void addJsonSchemaForBodyRequestInCaseItsNotPresent(CodegenParameter codegenParameter, RequestBody body) {
if (codegenParameter.jsonSchema == null)
codegenParameter.jsonSchema = Json.pretty(body);

View File

@ -29,6 +29,7 @@ import io.swagger.v3.oas.models.tags.Tag;
import org.apache.commons.io.FileUtils;
import org.openapitools.codegen.api.TemplatePathLocator;
import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.ignore.CodegenIgnoreProcessor;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
@ -239,6 +240,7 @@ public class PythonClientCodegen extends AbstractPythonCodegen {
// When the 'additionalProperties' keyword is not present in a OAS schema, allow
// undeclared properties. This is compliant with the JSON schema specification.
this.setDisallowAdditionalPropertiesIfNotPresent(false);
GlobalSettings.setProperty("x-disallow-additional-properties-if-not-present", "false");
// this may set datatype right for additional properties
instantiationTypes.put("map", "dict");
@ -1063,19 +1065,9 @@ public class PythonClientCodegen extends AbstractPythonCodegen {
if (cp.isPrimitiveType && unaliasedSchema.get$ref() != null) {
cp.complexType = cp.dataType;
}
setAdditionalPropsAndItemsVarNames(cp);
return cp;
}
private void setAdditionalPropsAndItemsVarNames(IJsonSchemaValidationProperties item) {
if (item.getAdditionalProperties() != null) {
item.getAdditionalProperties().setBaseName("additional_properties");
}
if (item.getItems() != null) {
item.getItems().setBaseName("items");
}
}
/**
* checks if the data should be classified as "string" in enum
* e.g. double in C# needs to be double-quoted (e.g. "2.8") by treating it as a string
@ -1089,6 +1081,10 @@ public class PythonClientCodegen extends AbstractPythonCodegen {
return "str".equals(dataType);
}
protected String getItemsName(Schema containingSchema, String containingSchemaName) {
return "items";
}
/**
* Update codegen property's enum by adding "enumVars" (with name and value)
*
@ -1510,7 +1506,6 @@ public class PythonClientCodegen extends AbstractPythonCodegen {
cm.setHasMultipleTypes(true);
}
Boolean isNotPythonModelSimpleModel = (ModelUtils.isComposedSchema(sc) || ModelUtils.isObjectSchema(sc) || ModelUtils.isMapSchema(sc));
setAdditionalPropsAndItemsVarNames(cm);
if (isNotPythonModelSimpleModel) {
return cm;
}
@ -2272,7 +2267,7 @@ public class PythonClientCodegen extends AbstractPythonCodegen {
if (addPropsSchema == null) {
return;
}
CodegenProperty addPropProp = fromProperty("", addPropsSchema, false, false);
CodegenProperty addPropProp = fromProperty(getAdditionalPropertiesName(), addPropsSchema, false, false);
property.setAdditionalProperties(addPropProp);
}

View File

@ -2592,7 +2592,7 @@ public class DefaultCodegenTest {
String modelName;
Schema sc;
CodegenModel cm;
CodegenProperty anyTypeSchema = codegen.fromProperty("", new Schema());
CodegenProperty anyTypeSchema = codegen.fromProperty("additional_properties", new Schema());
modelName = "AdditionalPropertiesUnset";
sc = openAPI.getComponents().getSchemas().get(modelName);
@ -2615,7 +2615,7 @@ public class DefaultCodegenTest {
modelName = "AdditionalPropertiesSchema";
sc = openAPI.getComponents().getSchemas().get(modelName);
cm = codegen.fromModel(modelName, sc);
CodegenProperty stringCp = codegen.fromProperty("", new Schema().type("string"));
CodegenProperty stringCp = codegen.fromProperty("additional_properties", new Schema().type("string"));
assertEquals(cm.getAdditionalProperties(), stringCp);
assertFalse(cm.getAdditionalPropertiesIsAnyType());
}
@ -2630,8 +2630,8 @@ public class DefaultCodegenTest {
String modelName;
Schema sc;
CodegenModel cm;
CodegenProperty anyTypeSchema = codegen.fromProperty("", new Schema());
CodegenProperty stringCp = codegen.fromProperty("", new Schema().type("string"));
CodegenProperty anyTypeSchema = codegen.fromProperty("additional_properties", new Schema());
CodegenProperty stringCp = codegen.fromProperty("additional_properties", new Schema().type("string"));
CodegenProperty mapWithAddPropsUnset;
CodegenProperty mapWithAddPropsTrue;
CodegenProperty mapWithAddPropsFalse;
@ -2691,8 +2691,8 @@ public class DefaultCodegenTest {
Operation operation;
CodegenOperation co;
CodegenProperty anyTypeSchema = codegen.fromProperty("", new Schema());
CodegenProperty stringCp = codegen.fromProperty("", new Schema().type("string"));
CodegenProperty anyTypeSchema = codegen.fromProperty("additional_properties", new Schema());
CodegenProperty stringCp = codegen.fromProperty("additional_properties", new Schema().type("string"));
CodegenParameter mapWithAddPropsUnset;
CodegenParameter mapWithAddPropsTrue;
CodegenParameter mapWithAddPropsFalse;
@ -2752,8 +2752,8 @@ public class DefaultCodegenTest {
Operation operation;
CodegenOperation co;
CodegenProperty anyTypeSchema = codegen.fromProperty("", new Schema());
CodegenProperty stringCp = codegen.fromProperty("", new Schema().type("string"));
CodegenProperty anyTypeSchema = codegen.fromProperty("additional_properties", new Schema());
CodegenProperty stringCp = codegen.fromProperty("additional_properties", new Schema().type("string"));
CodegenResponse mapWithAddPropsUnset;
CodegenResponse mapWithAddPropsTrue;
CodegenResponse mapWithAddPropsFalse;
@ -2808,7 +2808,7 @@ public class DefaultCodegenTest {
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
CodegenProperty anyTypeSchema = codegen.fromProperty("", new Schema());
CodegenProperty anyTypeSchema = codegen.fromProperty("additional_properties", new Schema());
Schema sc;
CodegenModel cm;

View File

@ -233,4 +233,30 @@ public class PythonClientTest {
String importValue = codegen.toModelImport("model_name");
Assert.assertEquals(importValue, "from openapi.client.model.model_name import ModelName");
}
@Test
public void testIdenticalSchemasHaveDifferentBasenames() {
final PythonClientCodegen codegen = new PythonClientCodegen();
Schema objSchema = new Schema();
objSchema.setType("object");
Schema addProp = new Schema();
addProp.setType("object");
objSchema.setAdditionalProperties(addProp);
Schema arraySchema = new ArraySchema();
Schema items = new Schema();
items.setType("object");
arraySchema.setItems(items);
CodegenProperty objSchemaProp = codegen.fromProperty("objSchemaProp", objSchema, false, false);
CodegenProperty arraySchemaProp = codegen.fromProperty("arraySchemaProp", arraySchema, false, false);
Assert.assertEquals(objSchemaProp.getAdditionalProperties().baseName, "additional_properties");
Assert.assertEquals(arraySchemaProp.getItems().baseName, "items");
CodegenModel objSchemaModel = codegen.fromModel("objSchemaModel", objSchema);
CodegenModel arraySchemaModel = codegen.fromModel("arraySchemaModel", arraySchema);
Assert.assertEquals(objSchemaModel.getAdditionalProperties().baseName, "additional_properties");
Assert.assertEquals(arraySchemaModel.getItems().baseName, "items");
}
}