Skip to content

Commit 6236d53

Browse files
authored
Merge pull request #1672 from swagger-api/issue1658
fix exception while file do not have '/'
2 parents 6a82e92 + 4fe4669 commit 6236d53

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
125125
if (isAnExternalRefFormat(ref)) {
126126
if (!ref.equals(RefFormat.URL)) {
127127
String schemaFullRef = schema.get$ref();
128-
String parent = file.substring(0, file.lastIndexOf('/'));
128+
String parent = (file.contains("/")) ? file.substring(0, file.lastIndexOf('/')) : "";
129129
if (!parent.isEmpty()) {
130130
if (schemaFullRef.contains("#/")) {
131131
String[] parts = schemaFullRef.split("#/");

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

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

87+
@Test
88+
public void testIssue1658() throws Exception{
89+
ParseOptions options = new ParseOptions();
90+
options.setResolve(true);
91+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/issue-1658/issue1658.yaml", null, options);
92+
93+
Assert.assertNotNull(result);
94+
Assert.assertNotNull(result.getOpenAPI());
95+
OpenAPI openAPI = result.getOpenAPI();
96+
assertTrue(result.getMessages().size() == 0);
97+
assertFalse(result.getMessages().contains("String index out of range: -1"));
98+
assertEquals(openAPI.getComponents().getSchemas().get("ref1").get$ref(), "#/components/schemas/ref2");
99+
assertTrue(openAPI.getComponents().getSchemas().get("ref2") != null);
100+
assertTrue(openAPI.getComponents().getSchemas().get("ref2").get$ref() == null);
101+
102+
}
103+
87104
@Test
88105
public void testIssue1644_NullValue() throws Exception{
89106
ParseOptions options = new ParseOptions();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
components:
2+
schemas:
3+
ref2:
4+
type: object
5+
properties:
6+
status:
7+
type: string
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
paths:
2+
healthCheck:
3+
get:
4+
summary: Gets service health status
5+
responses:
6+
'200':
7+
description: Success status
8+
content:
9+
application/json:
10+
schema:
11+
$ref: '#/components/schemas/responseBase'
12+
13+
14+
components:
15+
schemas:
16+
responseBase:
17+
$ref: '#/components/schemas/ref1'
18+
19+
ref1:
20+
$ref: './api2.yaml#/components/schemas/ref2'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Demo
4+
description: Demo to crash parser
5+
version: 1.0.0
6+
7+
paths:
8+
/internal/v1/hc:
9+
$ref: './apis.yaml#/paths/healthCheck'

0 commit comments

Comments
 (0)