Skip to content

Commit

Permalink
support resolve/resolveFully with no location
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed May 23, 2022
1 parent 06c8649 commit 2d57932
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DereferencerContext {
protected final OpenAPI openApi;

protected final List<AuthorizationValue> auths;
protected final String rootUri;
protected String rootUri;
protected final ParseOptions parseOptions;
protected String providedBaseUri;
protected SwaggerParseResult swaggerParseResult;
Expand Down Expand Up @@ -114,4 +114,9 @@ public DereferencerContext referenceSet(Map<String, Reference> referenceSet) {
this.referenceSet = referenceSet;
return this;
}

public DereferencerContext rootUri(String rootUri) {
this.rootUri = rootUri;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.swagger.v3.parser.reference;

import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.core.util.Json31;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -42,12 +45,27 @@ public void dereference(DereferencerContext context, Iterator<OpenAPIDereference
return;
}

// Set<Object> visitedSet = new HashSet<>();
LinkedHashMap<String, Reference> refSet = new LinkedHashMap<>();
LinkedHashSet<String> msgs = new LinkedHashSet<>();
if (StringUtils.isBlank(context.getRootUri())) {
context.rootUri("local");
context.currentUri("local");
}
if (context.getRootUri().equals("local")) {
Reference localReference = new Reference()
.referenceSet(refSet)
.uri(context.getCurrentUri())
.messages(msgs)
.jsonNode(Json31.mapper().convertValue(openAPI, JsonNode.class))
.auths(context.getAuths());

refSet.put("local", localReference);
}

Reference reference = new Reference()
.referenceSet(new LinkedHashMap<>())
.referenceSet(refSet)
.uri(context.getCurrentUri())
.messages(new LinkedHashSet<>())
// .visitedSet(visitedSet)
.messages(msgs)
.auths(context.getAuths());

Traverser traverser = buildTraverser(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ private void setUpWireMockServer() throws IOException {
.withHeader("Content-type", "application/yaml")
.withBody(pathFile
.getBytes(StandardCharsets.UTF_8))));

pathFile = FileUtils.readFileToString(new File("src/test/resources/3.1.0/dereference/pathItem/internal-indirections/root.json"));
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));

WireMock.stubFor(get(urlPathMatching("/internal-indirections/root.json"))
.willReturn(aResponse()
.withStatus(HttpURLConnection.HTTP_OK)
.withHeader("Content-type", "application/json")
.withBody(pathFile
.getBytes(StandardCharsets.UTF_8))));

}

@AfterClass
Expand Down Expand Up @@ -450,4 +461,47 @@ public void testPathItemInternalExternalRef() throws Exception {
" description: path item description\n" +
" get: {}\n");
}

@Test
public void testPathItemInternalNoLocation() throws Exception {
ParseOptions p = new ParseOptions();
p.setResolve(true);
p.setResolveFully(true);

String con = "{\n" +
" \"openapi\": \"3.1.0\",\n" +
" \"paths\": {\n" +
" \"/path1\": {\n" +
" \"$ref\": \"#/paths/~1path2\"\n" +
" },\n" +
" \"/path2\": {\n" +
" \"$ref\": \"#/paths/~1path3\"\n" +
" },\n" +
" \"/path3\": {\n" +
" \"summary\": \"path item summary\",\n" +
" \"description\": \"path item description\",\n" +
" \"get\": {}\n" +
" }\n" +
" }\n" +
"}\n";
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readContents(con, null, p);
assertNotNull(swaggerParseResult.getOpenAPI());
assertFalse(swaggerParseResult.getMessages().isEmpty());
assertEquals(Yaml31.pretty(swaggerParseResult.getOpenAPI()), "openapi: 3.1.0\n" +
"servers:\n" +
"- url: /\n" +
"paths:\n" +
" /path1:\n" +
" summary: path item summary\n" +
" description: path item description\n" +
" get: {}\n" +
" /path2:\n" +
" summary: path item summary\n" +
" description: path item description\n" +
" get: {}\n" +
" /path3:\n" +
" summary: path item summary\n" +
" description: path item description\n" +
" get: {}\n");
}
}

0 comments on commit 2d57932

Please sign in to comment.