Skip to content

Commit

Permalink
Test and fix swagger-api#750 by reusing ExternalRefProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Apr 28, 2024
1 parent be2301b commit 290223e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.models.auth.AuthorizationValue;
import io.swagger.models.refs.RefFormat;
import io.swagger.parser.processors.ExternalRefProcessor;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -80,45 +81,7 @@ public static String readExternalUrlRef(String file, RefFormat refFormat, List<A
}

public static String buildUrl(String rootPath, String relativePath) {
String[] rootPathParts = rootPath.split("/");
String [] relPathParts = relativePath.split("/");

if(rootPath == null || relativePath == null) {
return null;
}

int trimRoot = 0;
int trimRel = 0;

if(!"".equals(rootPathParts[rootPathParts.length - 1])) {
trimRoot = 1;
}
for(int i = 0; i < rootPathParts.length; i++) {
if("".equals(rootPathParts[i])) {
trimRel += 1;
}
else {
break;
}
}
for(int i = 0; i < relPathParts.length; i ++) {
if(".".equals(relPathParts[i])) {
trimRel += 1;
}
else if ("..".equals(relPathParts[i])) {
trimRel += 1;
}
}

String [] outputParts = new String[rootPathParts.length + relPathParts.length - trimRoot - trimRel];
System.arraycopy(rootPathParts, 0, outputParts, 0, rootPathParts.length - trimRoot);
System.arraycopy(relPathParts,
trimRel,
outputParts,
rootPathParts.length - trimRoot + trimRel - 1,
relPathParts.length - trimRel);

return StringUtils.join(outputParts, "/");
return ExternalRefProcessor.join(rootPath, relativePath);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,16 @@ public void testPathJoin1() {
// relative locations
assertEquals(ExternalRefProcessor.join("./foo#/definitions/Foo", "./bar#/definitions/Bar"), "./bar#/definitions/Bar");
}

@Test
public void testRelativeRef() {
// Test for #750
assertEquals(
RefUtils.buildUrl(
"https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/storage/resource-manager/Microsoft.Storage/stable/2018-02-01/storage.json",
"../../../../../common-types/resource-management/v1/types.json"
),
"https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/common-types/resource-management/v1/types.json"
);
}
}

0 comments on commit 290223e

Please sign in to comment.