Skip to content

Commit

Permalink
Fix issue #1501: External schema name is correctly constructed based …
Browse files Browse the repository at this point in the history
…on all existing models
  • Loading branch information
BabisK committed Dec 2, 2020
1 parent f5fdccb commit 59f0e27
Showing 1 changed file with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ public ExternalRefProcessor(ResolverCache cache, OpenAPI openAPI) {
this.openAPI = openAPI;
}

private String finalNameRec(Map<String, Schema> schemas, String possiblyConflictingDefinitionName, Schema newScema,
int iteration) {
String tryName =
iteration == 0 ? possiblyConflictingDefinitionName : possiblyConflictingDefinitionName + "_" + iteration;
Schema existingModel = schemas.get(tryName);
if (existingModel != null) {
if (existingModel.get$ref() != null) {
// use the new model
existingModel = null;
} else if (!newScema.equals(existingModel)) {
LOGGER.debug("A model for " + existingModel + " already exists");
return finalNameRec(schemas, possiblyConflictingDefinitionName, newScema, ++iteration);
}
}
return tryName;
}

public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if(renamedRef != null) {
Expand All @@ -75,34 +92,13 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
}

final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
String tryName = null;
Schema existingModel = schemas.get(possiblyConflictingDefinitionName);

if (existingModel != null) {
LOGGER.warn("A model for " + existingModel + " already exists");
if(existingModel.get$ref() != null) {
// use the new model
existingModel = null;
}else{
if (!schema.equals(existingModel)){
//We add a number at the end of the definition name
int i = 2;
for (String name : schemas.keySet()) {
if (name.equals(possiblyConflictingDefinitionName)) {
tryName = possiblyConflictingDefinitionName + "_" + i;
existingModel = schemas.get(tryName);
i++;
}
}
}
}
}
if (StringUtils.isNotBlank(tryName)){
newRef = tryName;
}else{
newRef = possiblyConflictingDefinitionName;
}
newRef = finalNameRec(schemas, possiblyConflictingDefinitionName, schema, 0);
cache.putRenamedRef($ref, newRef);
Schema existingModel = schemas.get(newRef);
if(existingModel != null && existingModel.get$ref() != null) {
// use the new model
existingModel = null;
}

if(existingModel == null) {
// don't overwrite existing model reference
Expand Down

0 comments on commit 59f0e27

Please sign in to comment.