Skip to content

Commit 2d57932

Browse files
committed
support resolve/resolveFully with no location
1 parent 06c8649 commit 2d57932

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/DereferencerContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class DereferencerContext {
1414
protected final OpenAPI openApi;
1515

1616
protected final List<AuthorizationValue> auths;
17-
protected final String rootUri;
17+
protected String rootUri;
1818
protected final ParseOptions parseOptions;
1919
protected String providedBaseUri;
2020
protected SwaggerParseResult swaggerParseResult;
@@ -114,4 +114,9 @@ public DereferencerContext referenceSet(Map<String, Reference> referenceSet) {
114114
this.referenceSet = referenceSet;
115115
return this;
116116
}
117+
118+
public DereferencerContext rootUri(String rootUri) {
119+
this.rootUri = rootUri;
120+
return this;
121+
}
117122
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/OpenAPIDereferencer31.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.swagger.v3.parser.reference;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import io.swagger.v3.core.util.Json31;
35
import io.swagger.v3.oas.models.OpenAPI;
46
import io.swagger.v3.parser.core.models.SwaggerParseResult;
7+
import org.apache.commons.lang3.StringUtils;
58

69
import java.util.HashMap;
710
import java.util.HashSet;
@@ -42,12 +45,27 @@ public void dereference(DereferencerContext context, Iterator<OpenAPIDereference
4245
return;
4346
}
4447

45-
// Set<Object> visitedSet = new HashSet<>();
48+
LinkedHashMap<String, Reference> refSet = new LinkedHashMap<>();
49+
LinkedHashSet<String> msgs = new LinkedHashSet<>();
50+
if (StringUtils.isBlank(context.getRootUri())) {
51+
context.rootUri("local");
52+
context.currentUri("local");
53+
}
54+
if (context.getRootUri().equals("local")) {
55+
Reference localReference = new Reference()
56+
.referenceSet(refSet)
57+
.uri(context.getCurrentUri())
58+
.messages(msgs)
59+
.jsonNode(Json31.mapper().convertValue(openAPI, JsonNode.class))
60+
.auths(context.getAuths());
61+
62+
refSet.put("local", localReference);
63+
}
64+
4665
Reference reference = new Reference()
47-
.referenceSet(new LinkedHashMap<>())
66+
.referenceSet(refSet)
4867
.uri(context.getCurrentUri())
49-
.messages(new LinkedHashSet<>())
50-
// .visitedSet(visitedSet)
68+
.messages(msgs)
5169
.auths(context.getAuths());
5270

5371
Traverser traverser = buildTraverser(context);

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserPathItemResolveFullyTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ private void setUpWireMockServer() throws IOException {
214214
.withHeader("Content-type", "application/yaml")
215215
.withBody(pathFile
216216
.getBytes(StandardCharsets.UTF_8))));
217+
218+
pathFile = FileUtils.readFileToString(new File("src/test/resources/3.1.0/dereference/pathItem/internal-indirections/root.json"));
219+
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
220+
221+
WireMock.stubFor(get(urlPathMatching("/internal-indirections/root.json"))
222+
.willReturn(aResponse()
223+
.withStatus(HttpURLConnection.HTTP_OK)
224+
.withHeader("Content-type", "application/json")
225+
.withBody(pathFile
226+
.getBytes(StandardCharsets.UTF_8))));
227+
217228
}
218229

219230
@AfterClass
@@ -450,4 +461,47 @@ public void testPathItemInternalExternalRef() throws Exception {
450461
" description: path item description\n" +
451462
" get: {}\n");
452463
}
464+
465+
@Test
466+
public void testPathItemInternalNoLocation() throws Exception {
467+
ParseOptions p = new ParseOptions();
468+
p.setResolve(true);
469+
p.setResolveFully(true);
470+
471+
String con = "{\n" +
472+
" \"openapi\": \"3.1.0\",\n" +
473+
" \"paths\": {\n" +
474+
" \"/path1\": {\n" +
475+
" \"$ref\": \"#/paths/~1path2\"\n" +
476+
" },\n" +
477+
" \"/path2\": {\n" +
478+
" \"$ref\": \"#/paths/~1path3\"\n" +
479+
" },\n" +
480+
" \"/path3\": {\n" +
481+
" \"summary\": \"path item summary\",\n" +
482+
" \"description\": \"path item description\",\n" +
483+
" \"get\": {}\n" +
484+
" }\n" +
485+
" }\n" +
486+
"}\n";
487+
SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readContents(con, null, p);
488+
assertNotNull(swaggerParseResult.getOpenAPI());
489+
assertFalse(swaggerParseResult.getMessages().isEmpty());
490+
assertEquals(Yaml31.pretty(swaggerParseResult.getOpenAPI()), "openapi: 3.1.0\n" +
491+
"servers:\n" +
492+
"- url: /\n" +
493+
"paths:\n" +
494+
" /path1:\n" +
495+
" summary: path item summary\n" +
496+
" description: path item description\n" +
497+
" get: {}\n" +
498+
" /path2:\n" +
499+
" summary: path item summary\n" +
500+
" description: path item description\n" +
501+
" get: {}\n" +
502+
" /path3:\n" +
503+
" summary: path item summary\n" +
504+
" description: path item description\n" +
505+
" get: {}\n");
506+
}
453507
}

0 commit comments

Comments
 (0)