Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 8857665

Browse files
committed
Adds java fix for additionalProperties and items names
1 parent ab7f915 commit 8857665

2 files changed

Lines changed: 36 additions & 23 deletions

File tree

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,7 +2934,8 @@ public CodegenModel fromModel(String name, Schema schema) {
29342934
m.setFormat(schema.getFormat());
29352935
m.setComposedSchemas(getComposedSchemas(schema));
29362936
if (ModelUtils.isArraySchema(schema)) {
2937-
CodegenProperty arrayProperty = fromProperty(name, schema, false);
2937+
String itemName = getItemsNameForModel(name);
2938+
CodegenProperty arrayProperty = fromProperty(itemName, schema, false);
29382939
m.setItems(arrayProperty.items);
29392940
m.arrayModelType = arrayProperty.complexType;
29402941
addParentContainer(m, name, schema);
@@ -3032,17 +3033,17 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper
30323033
if (schema.getAdditionalProperties() == null) {
30333034
if (!disallowAdditionalPropertiesIfNotPresent) {
30343035
isAdditionalPropertiesTrue = true;
3035-
addPropProp = fromProperty("", new Schema(), false);
3036+
addPropProp = fromProperty(getAdditionalPropertiesName(), new Schema(), false);
30363037
additionalPropertiesIsAnyType = true;
30373038
}
30383039
} else if (schema.getAdditionalProperties() instanceof Boolean) {
30393040
if (Boolean.TRUE.equals(schema.getAdditionalProperties())) {
30403041
isAdditionalPropertiesTrue = true;
3041-
addPropProp = fromProperty("", new Schema(), false);
3042+
addPropProp = fromProperty(getAdditionalPropertiesName(), new Schema(), false);
30423043
additionalPropertiesIsAnyType = true;
30433044
}
30443045
} else {
3045-
addPropProp = fromProperty("", (Schema) schema.getAdditionalProperties(), false);
3046+
addPropProp = fromProperty(getAdditionalPropertiesName(), (Schema) schema.getAdditionalProperties(), false);
30463047
if (ModelUtils.isAnyType((Schema) schema.getAdditionalProperties())) {
30473048
additionalPropertiesIsAnyType = true;
30483049
}
@@ -3850,13 +3851,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
38503851
}
38513852

38523853
// handle inner property
3853-
String itemName = null;
3854-
if (p.getExtensions() != null && p.getExtensions().get("x-item-name") != null) {
3855-
itemName = p.getExtensions().get("x-item-name").toString();
3856-
}
3857-
if (itemName == null) {
3858-
itemName = property.name;
3859-
}
3854+
String itemName = getItemsNameForProp(p, name);
38603855
ArraySchema arraySchema = (ArraySchema) p;
38613856
Schema innerSchema = unaliasSchema(getSchemaItems(arraySchema));
38623857
CodegenProperty cp = fromProperty(itemName, innerSchema, false);
@@ -7914,6 +7909,27 @@ public List<VendorExtension> getSupportedVendorExtensions() {
79147909
*/
79157910
protected String handleSpecialCharacters(String name) { return name; }
79167911

7912+
public String getItemsNameForProp(Schema containingSchema, String containingSchemaName) {
7913+
String itemName = null;
7914+
if (containingSchema.getExtensions() != null && containingSchema.getExtensions().get("x-item-name") != null) {
7915+
itemName = containingSchema.getExtensions().get("x-item-name").toString();
7916+
}
7917+
if (itemName == null) {
7918+
itemName = toVarName(containingSchemaName);
7919+
}
7920+
return itemName;
7921+
}
7922+
7923+
public String getItemsNameForModel(String containingSchemaName) {
7924+
return containingSchemaName;
7925+
}
7926+
7927+
7928+
public String getAdditionalPropertiesName() {
7929+
return "additional_properties";
7930+
}
7931+
7932+
79177933
/**
79187934
* Used to ensure that null or Schema is returned given an input Boolean/Schema/null
79197935
* This will be used in openapi 3.1.0 spec processing to ensure that Booleans become Schemas

modules/openapi-json-schema-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,19 +1063,9 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
10631063
if (cp.isPrimitiveType && unaliasedSchema.get$ref() != null) {
10641064
cp.complexType = cp.dataType;
10651065
}
1066-
setAdditionalPropsAndItemsVarNames(cp);
10671066
return cp;
10681067
}
10691068

1070-
private void setAdditionalPropsAndItemsVarNames(IJsonSchemaValidationProperties item) {
1071-
if (item.getAdditionalProperties() != null) {
1072-
item.getAdditionalProperties().setBaseName("additional_properties");
1073-
}
1074-
if (item.getItems() != null) {
1075-
item.getItems().setBaseName("items");
1076-
}
1077-
}
1078-
10791069
/**
10801070
* checks if the data should be classified as "string" in enum
10811071
* e.g. double in C# needs to be double-quoted (e.g. "2.8") by treating it as a string
@@ -1089,6 +1079,14 @@ public boolean isDataTypeString(String dataType) {
10891079
return "str".equals(dataType);
10901080
}
10911081

1082+
public String getItemsNameForProp(Schema containingSchema, String containingSchemaName) {
1083+
return "items";
1084+
}
1085+
1086+
public String getItemsNameForModel(String containingSchemaName) {
1087+
return "items";
1088+
}
1089+
10921090
/**
10931091
* Update codegen property's enum by adding "enumVars" (with name and value)
10941092
*
@@ -1510,7 +1508,6 @@ public CodegenModel fromModel(String name, Schema sc) {
15101508
cm.setHasMultipleTypes(true);
15111509
}
15121510
Boolean isNotPythonModelSimpleModel = (ModelUtils.isComposedSchema(sc) || ModelUtils.isObjectSchema(sc) || ModelUtils.isMapSchema(sc));
1513-
setAdditionalPropsAndItemsVarNames(cm);
15141511
if (isNotPythonModelSimpleModel) {
15151512
return cm;
15161513
}
@@ -2272,7 +2269,7 @@ protected void setAddProps(Schema schema, IJsonSchemaValidationProperties proper
22722269
if (addPropsSchema == null) {
22732270
return;
22742271
}
2275-
CodegenProperty addPropProp = fromProperty("", addPropsSchema, false, false);
2272+
CodegenProperty addPropProp = fromProperty(getAdditionalPropertiesName(), addPropsSchema, false, false);
22762273
property.setAdditionalProperties(addPropProp);
22772274
}
22782275

0 commit comments

Comments
 (0)