Skip to content

Commit 5de91aa

Browse files
authored
Merge pull request #1547 from swagger-api/issue-251
fix issue 251 - broken domain resolving
2 parents 0b76992 + a3c193d commit 5de91aa

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
109109
if (schema.get$ref() != null) {
110110
RefFormat ref = computeRefFormat(schema.get$ref());
111111
if (isAnExternalRefFormat(ref)) {
112-
String schemaFullRef = schema.get$ref();
113-
String parent = file.substring(0, file.lastIndexOf(File.separatorChar));
114-
if (!parent.isEmpty()) {
115-
schemaFullRef = Paths.get(parent, schemaFullRef).normalize().toString();
112+
if (!ref.equals(RefFormat.URL)) {
113+
String schemaFullRef = schema.get$ref();
114+
String parent = file.substring(0, file.lastIndexOf(File.separatorChar));
115+
if (!parent.isEmpty()) {
116+
schemaFullRef = Paths.get(parent, schemaFullRef).normalize().toString();
117+
}
118+
schema.set$ref(processRefToExternalSchema(schemaFullRef, ref));
116119
}
117-
schema.set$ref(processRefToExternalSchema(schemaFullRef, ref));
118120
} else {
119121
processRefToExternalSchema(file + schema.get$ref(), RefFormat.RELATIVE);
120122
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ public class OpenAPIV3ParserTest {
8383
protected int serverPort = getDynamicPort();
8484
protected WireMockServer wireMockServer;
8585

86+
@Test
87+
public void testIssue251() throws IOException {
88+
String pathFile = FileUtils.readFileToString(new File("src/test/resources/domain.yaml"), "UTF-8");
89+
WireMock.stubFor(get(urlPathMatching("/domain"))
90+
.willReturn(aResponse()
91+
.withStatus(HttpURLConnection.HTTP_OK)
92+
.withHeader("Content-type", "application/json")
93+
.withBody(pathFile
94+
.getBytes(StandardCharsets.UTF_8))));
95+
96+
pathFile = FileUtils.readFileToString(new File("src/test/resources/issue251.yaml"), "UTF-8");
97+
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
98+
99+
OpenAPIV3Parser parser = new OpenAPIV3Parser();
100+
ParseOptions options = new ParseOptions();
101+
options.setResolve(true);
102+
103+
final SwaggerParseResult parseResult = parser.readContents(pathFile, null, options);
104+
105+
assertEquals(parseResult.getMessages().size(), 0);
106+
assertTrue(parseResult.getOpenAPI().getComponents().getSchemas().size() == 2);
107+
assertTrue(parseResult.getOpenAPI().getPaths().get("/parse").getGet().getParameters().get(0).getSchema().get$ref().equals("#/components/schemas/Parse"));
108+
}
109+
86110
@Test
87111
public void testCantReadDeepProperties() {
88112
OpenAPIV3Parser parser = new OpenAPIV3Parser();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Parser Test
4+
version: '1.0'
5+
components:
6+
schemas:
7+
Parse:
8+
$ref: '#/components/schemas/ParseEnum'
9+
ParseEnum:
10+
title: Parse It
11+
description: Can it parse it?
12+
type: string
13+
enum:
14+
- Yes
15+
- No
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
openapi: 3.0.3
2+
info:
3+
version: "1.0.0"
4+
title: parse-api
5+
description: Test swagger-parser
6+
paths:
7+
/parse:
8+
get:
9+
description: Parser test
10+
operationId: getParse
11+
parameters:
12+
- in: query
13+
name: parse
14+
required: true
15+
schema:
16+
$ref: 'http://localhost:${dynamicPort}/domain#/components/schemas/Parse'
17+
responses:
18+
'200':
19+
description: OK

0 commit comments

Comments
 (0)