Skip to content

Relative references from a file in a sub-folder to other folders do not resolve #305

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

Closed
KaelenProctor opened this issue Oct 24, 2016 · 5 comments
Milestone

Comments

@KaelenProctor
Copy link

KaelenProctor commented Oct 24, 2016

Originally from swagger-codegen swagger-api/swagger-codegen#3614

If you have a directory structure like

swagger.yaml
-folder1
 -file1.yaml
 -folder2
  -file2.yaml
-folder3
 -file3.yaml

then from file1.yaml it is unable to resolve references to file3.yaml or file2.yaml.

For the case in which file1 references file2, the exception thrown is:

Exception in thread "main" java.lang.RuntimeException: Unable to load RELATIVE ref: ../folder2/file2.yaml
        at io.swagger.parser.util.RefUtils.readExternalRef(RefUtils.java:140)
        at io.swagger.parser.ResolverCache.loadRef(ResolverCache.java:96)
        at io.swagger.parser.processors.ExternalRefProcessor.processRefToExternalDefinition(ExternalRefProcessor.java:30)
        at io.swagger.parser.processors.ExternalRefProcessor.processRefToExternalDefinition(ExternalRefProcessor.java:69)
        at io.swagger.parser.processors.PropertyProcessor.processRefProperty(PropertyProcessor.java:33)
        at io.swagger.parser.processors.PropertyProcessor.processProperty(PropertyProcessor.java:21)
        at io.swagger.parser.processors.PropertyProcessor.processObjectProperty(PropertyProcessor.java:59)
        at io.swagger.parser.processors.PropertyProcessor.processProperty(PropertyProcessor.java:27)
        at io.swagger.parser.processors.ResponseProcessor.processResponse(ResponseProcessor.java:21)
        at io.swagger.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:45)
        at io.swagger.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:89)
        at io.swagger.parser.SwaggerResolver.resolve(SwaggerResolver.java:40)
        at io.swagger.parser.SwaggerParser.read(SwaggerParser.java:66)
        at io.swagger.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:390)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:221)
        at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:36)
Caused by: java.lang.RuntimeException: Could not find ../folder2/file2.yaml on the classpath
        at io.swagger.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:31)
        at io.swagger.parser.util.RefUtils.readExternalRef(RefUtils.java:135)
        ... 15 more

I created a suite of tests that I believe illustrate the problem, and attached it swagger-relative-file-references.zip.

The test cases that compare "./file1.yaml" vs "file.yaml" can likely be ignored, I created them for my own education to verify which formats of a relative reference were acceptable.

@fehguy
Copy link
Contributor

fehguy commented Oct 25, 2016

Thank you for submitting this. Are you testing with master or the latest release?

@fehguy
Copy link
Contributor

fehguy commented Oct 25, 2016

In the future, consider sending a PR with failing tests. Then we're all testing the same thing. But since you did organize your tests, here is my test feedback:

Details:

Case 3:
If you're going to reference $ref: './paging.yaml' and the contents are this:

Paging:
  type: object
  properties:
    total_items:
      type: integer

the correct reference is actually this:

$ref: './paging.yaml#/Paging'

@KaelenProctor
Copy link
Author

@fehguy These cases were tested with release 2.2.1 of swagger-codegen. I've also tested with a more recent master version, 4f9ee0f to be specific, and the same behaviour occurs.

I was planning on making a PR soon, but I haven't written Java in several years so I have a bit of ramping up to do =)

@fehguy
Copy link
Contributor

fehguy commented Oct 27, 2016

So thank you for getting these tests in. It was a long time coming, and I've gone through and carefully recreated your scenarios. This will go in the next parser release, then into codegen.

@fehguy fehguy closed this as completed Oct 27, 2016
@fehguy fehguy modified the milestone: v1.0.23 Oct 28, 2016
@maheshmahadevan
Copy link

Has this been fixed , i am still facing the problem with swagger-codegen v2.3.0

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

3 participants