Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser does not resolve $ref pointing to relative locations on classpath #1133

Closed
scottr-ad opened this issue Jun 21, 2019 · 16 comments
Closed

Comments

@scottr-ad
Copy link
Contributor

scottr-ad commented Jun 21, 2019

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

@abangfadli
Copy link

Did you manage to find out the workaround for this?

@scottr-ad
Copy link
Contributor Author

@abangfadli Yes, the attached swagger-parser-unified-diff.txt file lists code changes I made to 2.0.13-SNAPSHOT to get it working. I've also added a change (PathUtilTest.java) to a test that the changes fail.

Without the code changes above, a workaround is to keep schema files at the root of the classpath (not in a jar file) - $ref files can correctly resolve sub-paths from there.

@scottr-ad
Copy link
Contributor Author

Pull request #1134 raised to fix this issue. Could one of the regular maintainers review that at your convenience?

@gracekarina
Copy link
Contributor

Hi @scottr-ad yes I'll have a look, and I need to test it also. thanks a lot for this PR!

@scottr-ad
Copy link
Contributor Author

Hi @gracekarina Thanks for taking a look at this PR. I was hoping to get this fix into the next release, if possible. Have you had a chance to test it, or recommend any changes?

@gracekarina
Copy link
Contributor

closed by #1134

@ChubbyArse
Copy link

Is this the same issue as I've posted in the Atlassian Validation repo? https://bitbucket.org/atlassian/swagger-request-validator/issues/231/relative-refs-not-being-loaded-when

@scottr-ad
Copy link
Contributor Author

@ChubbyArse - it could be. Try adding swagger-parser v 2.0.14-SNAPSHOT directly to your maven or gradle build file to override the version swagger-request-validator is pulling in, and see if that fixes your problem.

@ChubbyArse
Copy link

@scottr-ad - Is that publically available? I've got this and it's failing to find anything:
[WARNING] The POM for io.swagger.parser.v3:swagger-parser-v3:jar:2.0.14-SNAPSHOT is missing, no dependency information available

 <!-- https://mvnrepository.com/artifact/io.swagger.parser.v3/swagger-parser-v3 -->
        <dependency>
            <groupId>io.swagger.parser.v3</groupId>
            <artifactId>swagger-parser-v3</artifactId>
            <version>2.0.14-SNAPSHOT</version>
        </dependency>

@scottr-ad
Copy link
Contributor Author

You could try building it locally using mvn clean install and see if that works.

@gracekarina Do you know when you plan to release 2.0.14?

@ChubbyArse
Copy link

@scottr-ad Do you know the repo that is serving the 2.0.14-SNAPSHOT version? I may need to add it to get it?

@ChubbyArse
Copy link

Found it but we aren't allowed access to SNAPSHOTs so were dependent on the release of 2.0.14

@gracekarina
Copy link
Contributor

Hi, @scottr-ad @ChubbyArse we are planning a release this week.

@ChubbyArse
Copy link

@gracekarina - Thanks will look forward to it. Quick question will the SNAPSHOT dependencies be set to release versions, as we aren't able to pull in snapshots - currently in master there is:

<swagger-parser-v2-version>1.0.46-SNAPSHOT</swagger-parser-v2-version>
<swagger-core-version>2.0.9-SNAPSHOT</swagger-core-version>

@gracekarina
Copy link
Contributor

Hi @ChubbyArse, yes, in release version all dependencies will be set to release version

@ChubbyArse
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants