Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.
Merged
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 @@ -2934,7 +2934,8 @@ public CodegenModel fromModel(String name, Schema schema) {
m.setFormat(schema.getFormat());
m.setComposedSchemas(getComposedSchemas(schema));
if (ModelUtils.isArraySchema(schema)) {
CodegenProperty arrayProperty = fromProperty(name, schema, false);
String itemName = getItemsName(null, name);
CodegenProperty arrayProperty = fromProperty(itemName, schema, false);
m.setItems(arrayProperty.items);
m.arrayModelType = arrayProperty.complexType;
addParentContainer(m, name, schema);
Expand Down Expand Up @@ -3032,17 +3033,17 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper
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;
}
Expand Down Expand Up @@ -3850,13 +3851,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
}

// 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);
Expand Down Expand Up @@ -7914,6 +7909,24 @@ public List<VendorExtension> getSupportedVendorExtensions() {
*/
protected String handleSpecialCharacters(String name) { return name; }

public String getItemsName(Schema containingSchema, String containingSchemaName) {
String itemName = null;
if (containingSchema != null) {
// 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);
}
// fromModel use case
return containingSchemaName;
}

public String getAdditionalPropertiesName() {
return "additional_properties";
}


/**
* Used to ensure that null or Schema is returned given an input Boolean/Schema/null
* This will be used in openapi 3.1.0 spec processing to ensure that Booleans become Schemas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

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;
Expand Down Expand Up @@ -239,6 +240,7 @@ public PythonClientCodegen() {
// 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");
Expand Down Expand Up @@ -1063,19 +1065,9 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
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
Expand All @@ -1089,6 +1081,10 @@ public boolean isDataTypeString(String dataType) {
return "str".equals(dataType);
}

public String getItemsName(Schema containingSchema, String containingSchemaName) {
return "items";
}

/**
* Update codegen property's enum by adding "enumVars" (with name and value)
*
Expand Down Expand Up @@ -1510,7 +1506,6 @@ public CodegenModel fromModel(String name, Schema sc) {
cm.setHasMultipleTypes(true);
}
Boolean isNotPythonModelSimpleModel = (ModelUtils.isComposedSchema(sc) || ModelUtils.isObjectSchema(sc) || ModelUtils.isMapSchema(sc));
setAdditionalPropsAndItemsVarNames(cm);
if (isNotPythonModelSimpleModel) {
return cm;
}
Expand Down Expand Up @@ -2272,7 +2267,7 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper
if (addPropsSchema == null) {
return;
}
CodegenProperty addPropProp = fromProperty("", addPropsSchema, false, false);
CodegenProperty addPropProp = fromProperty(getAdditionalPropertiesName(), addPropsSchema, false, false);
property.setAdditionalProperties(addPropProp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2592,7 +2592,7 @@ public void testAdditionalPropertiesPresentInModels() {
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);
Expand All @@ -2615,7 +2615,7 @@ public void testAdditionalPropertiesPresentInModels() {
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());
}
Expand All @@ -2630,8 +2630,8 @@ public void testAdditionalPropertiesPresentInModelProperties() {
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;
Expand Down Expand Up @@ -2691,8 +2691,8 @@ public void testAdditionalPropertiesPresentInParameters() {
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;
Expand Down Expand Up @@ -2752,8 +2752,8 @@ public void testAdditionalPropertiesPresentInResponses() {
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;
Expand Down Expand Up @@ -2808,7 +2808,7 @@ public void testAdditionalPropertiesAnyType() {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,30 @@ public void testImportWithQualifiedPackageName() throws Exception {
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");
}
}