swagger-parser: Parser does not resolve $ref pointing to relative locations on classpath

When parsing a schema file loaded from the classpath (either on file or in jar), the parser does not correctly resolve $ref elements pointing to other json or yaml files in relative path locations.

For example,

src/main/resources/
    |- schema/
        |- schema.yaml
        |- import/
            |- data_structures.yaml

schema.yaml

openapi: 3.0.2
info:
  title: Schema in sub-path
  version: 1-0
  description: 
paths:
  /test:
    post:
      operationId: doStuff
      requestBody:
        content:
          application/json:
            schema:
              $ref: 'import/data-structures.yaml#/Foo
      responses:
        '200':
          description: Successful result
          content:
            application/json:
              schema:
                $ref: 'import/data-structures.yaml#/Foo

data-structures.yaml

Foo:
  type: object
  (etc)

The parser should be able to resolve this relative $ref path when loading from the classpath, however it gives the following error message:

Unable to load RELATIVE ref: ./import/data-structures.yaml path: C:\schema

In debugging I observed two problems occurring:

  1. PathUtils fails to correctly identify the parent directory of schema files looked up on the classpath
  2. schema files located in jar files do not resolve to a parent directory, so ResolverCache needs an alternative way to read relative $ref elements from the classpath.

Attached is a test case demonstrating these issues ( as a zipped up maven build), a unified diff to fix the issues, and a test case fix

swagger-parser-bug-test.zip

swagger-parser-unified-diff.txt

PathUtilTest.java.txt

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Thanks to all involved in this one - our issue has been resolved with 2.0.14.