Skip to content

Commit 0202bac

Browse files
authored
fix parent name look up using schema name (#17807)
1 parent cbc3453 commit 0202bac

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
5151
public Set<String> oneOf = new TreeSet<>();
5252
public Set<String> allOf = new TreeSet<>();
5353

54-
// The schema name as written in the OpenAPI document.
54+
// The schema name as written in the OpenAPI document
55+
// If it's a reserved word, it will be escaped.
5556
public String name;
57+
// The original schema name as written in the OpenAPI document.
58+
public String schemaName;
5659
// The language-specific name of the class that implements this schema.
5760
// The name of the class is derived from the OpenAPI schema name with formatting rules applied.
5861
// The classname is derived from the OpenAPI schema name, with sanitization and escaping rules applied.
@@ -492,6 +495,15 @@ public void setName(String name) {
492495
this.name = name;
493496
}
494497

498+
public String getSchemaName() {
499+
return schemaName;
500+
}
501+
502+
public void setSchemaName(String schemaName) {
503+
this.schemaName = schemaName;
504+
}
505+
506+
495507
public List<CodegenProperty> getOptionalVars() {
496508
return optionalVars;
497509
}
@@ -1144,6 +1156,7 @@ public boolean equals(Object o) {
11441156
Objects.equals(oneOf, that.oneOf) &&
11451157
Objects.equals(allOf, that.allOf) &&
11461158
Objects.equals(name, that.name) &&
1159+
Objects.equals(schemaName, that.schemaName) &&
11471160
Objects.equals(classname, that.classname) &&
11481161
Objects.equals(title, that.title) &&
11491162
Objects.equals(description, that.description) &&
@@ -1192,7 +1205,7 @@ public boolean equals(Object o) {
11921205
@Override
11931206
public int hashCode() {
11941207
return Objects.hash(getParent(), getParentSchema(), getInterfaces(), getAllParents(), getParentModel(),
1195-
getInterfaceModels(), getChildren(), anyOf, oneOf, allOf, getName(), getClassname(), getTitle(),
1208+
getInterfaceModels(), getChildren(), anyOf, oneOf, allOf, getName(), getSchemaName(), getClassname(), getTitle(),
11961209
getDescription(), getClassVarName(), getModelJson(), getDataType(), getXmlPrefix(), getXmlNamespace(),
11971210
getXmlName(), getClassFilename(), getUnescapedDescription(), getDiscriminator(), getDefaultValue(),
11981211
getArrayModelType(), isAlias, isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble,
@@ -1214,6 +1227,7 @@ isAnyType, getComposedSchemas(), hasMultipleTypes, isDecimal, isUuid, isUri, req
12141227
public String toString() {
12151228
final StringBuilder sb = new StringBuilder("CodegenModel{");
12161229
sb.append("name='").append(name).append('\'');
1230+
sb.append(", schemaName='").append(schemaName).append('\'');
12171231
sb.append(", parent='").append(parent).append('\'');
12181232
sb.append(", parentSchema='").append(parentSchema).append('\'');
12191233
sb.append(", interfaces=").append(interfaces);

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -667,14 +667,16 @@ public Map<String, ModelsMap> updateAllModels(Map<String, ModelsMap> objs) {
667667
}
668668
parent.getChildren().add(cm);
669669
parent.hasChildren = true;
670-
Schema parentSchema = this.openAPI.getComponents().getSchemas().get(parent.name);
670+
Schema parentSchema = this.openAPI.getComponents().getSchemas().get(parent.schemaName);
671671
if (parentSchema == null) {
672-
throw new NullPointerException(parent.name + " in " + this.openAPI.getComponents().getSchemas());
673-
}
674-
if (parentSchema.getDiscriminator() == null) {
675-
parent = allModels.get(parent.getParent());
676-
} else {
672+
LOGGER.warn("Failed to look up parent schema: {}", parent.schemaName);
677673
parent = null;
674+
} else {
675+
if (parentSchema.getDiscriminator() == null) {
676+
parent = allModels.get(parent.getParent());
677+
} else {
678+
parent = null;
679+
}
678680
}
679681
}
680682
}
@@ -3122,6 +3124,7 @@ public CodegenModel fromModel(String name, Schema schema) {
31223124
} else {
31233125
m.name = name;
31243126
}
3127+
m.schemaName = name; // original schema name
31253128
m.title = escapeText(schema.getTitle());
31263129
m.description = escapeText(schema.getDescription());
31273130
m.unescapedDescription = schema.getDescription();

0 commit comments

Comments
 (0)