Repository: swagger-api/swagger-parser
Pull request #1672 (2022-03-08 14:27:58)

fix exception while file do not have '/'
fix and test for issue#1658
fix exception while file do not have '/'


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
index a675833a..466b09cf 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
@@ -125,7 +125,7 @@ public final class ExternalRefProcessor {
                 if (isAnExternalRefFormat(ref)) {
                     if (!ref.equals(RefFormat.URL)) {
                         String schemaFullRef = schema.get$ref();
-                        String parent = file.substring(0, file.lastIndexOf('/'));
+                        String parent = (file.contains("/")) ? file.substring(0, file.lastIndexOf('/')) : "";
                         if (!parent.isEmpty()) {
                             if (schemaFullRef.contains("#/")) {
                                 String[] parts = schemaFullRef.split("#/");


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
index 68ae4ad6..86db8b7e 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
@@ -84,6 +84,23 @@ public class OpenAPIV3ParserTest {
     protected int serverPort = getDynamicPort();
     protected WireMockServer wireMockServer;
 
+    @Test
+    public void testIssue1658() throws Exception{
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/issue-1658/issue1658.yaml", null, options);
+
+        Assert.assertNotNull(result);
+        Assert.assertNotNull(result.getOpenAPI());
+        OpenAPI openAPI = result.getOpenAPI();
+        assertTrue(result.getMessages().size() == 0);
+        assertFalse(result.getMessages().contains("String index out of range: -1"));
+        assertEquals(openAPI.getComponents().getSchemas().get("ref1").get$ref(), "#/components/schemas/ref2");
+        assertTrue(openAPI.getComponents().getSchemas().get("ref2") != null);
+        assertTrue(openAPI.getComponents().getSchemas().get("ref2").get$ref() == null);
+
+    }
+
     @Test
     public void testIssue1644_NullValue() throws Exception{
         ParseOptions options = new ParseOptions();
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1658/api2.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1658/api2.yaml
new file mode 100644
index 00000000..ed40c48e
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-1658/api2.yaml
@@ -0,0 +1,7 @@
+components:
+  schemas:
+    ref2:
+      type: object
+      properties:
+        status:
+          type: string 
\ No newline at end of file
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1658/apis.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1658/apis.yaml
new file mode 100644
index 00000000..ac126a9a
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-1658/apis.yaml
@@ -0,0 +1,20 @@
+paths:
+  healthCheck:
+    get:
+      summary: Gets service health status
+      responses:
+        '200':
+          description: Success status
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/responseBase'
+
+
+components:
+  schemas:
+    responseBase:
+      $ref: '#/components/schemas/ref1'
+
+    ref1:
+      $ref: './api2.yaml#/components/schemas/ref2' 
\ No newline at end of file
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1658/issue1658.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1658/issue1658.yaml
new file mode 100644
index 00000000..d0200972
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-1658/issue1658.yaml
@@ -0,0 +1,9 @@
+openapi: 3.0.0
+info:
+  title: Demo
+  description: Demo to crash parser
+  version: 1.0.0
+
+paths:
+  /internal/v1/hc:
+    $ref: './apis.yaml#/paths/healthCheck' 
\ No newline at end of file