Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support resolve/resolveFully with no location #1731

Merged
merged 1 commit into from
May 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -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);
Original file line number Diff line number Diff line change
@@ -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
@@ -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");
}
}