swagger-api / swagger-parser

Swagger Spec to Java POJOs
http://swagger.io
Apache License 2.0
778 stars 525 forks source link

Can not find $ref in nested directory #1292

Open iamchating opened 4 years ago

iamchating commented 4 years ago

Version: 2.0.16

The yaml project is in a structure as follows petstore.yml at root and pet.yml and def.yml in a sub directory named /pet and pet.yml $ref an object definition in def.yml

the redoc parses the project correctly, but swagger-parser gives an error

java.lang.RuntimeException: Unable to load RELATIVE ref: ./def.yml path: /private/tmp/yaml at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:239) at io.swagger.v3.parser.ResolverCache.loadRef(ResolverCache.java:119) at io.swagger.v3.parser.processors.ExternalRefProcessor.processRefToExternalSchema(ExternalRefProcessor.java:56) at io.swagger.v3.parser.processors.SchemaProcessor.processReferenceSchema(SchemaProcessor.java:202) at io.swagger.v3.parser.processors.SchemaProcessor.processSchema(SchemaProcessor.java:37) at io.swagger.v3.parser.processors.ResponseProcessor.processResponse(ResponseProcessor.java:52) at io.swagger.v3.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:67) at io.swagger.v3.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:83) at io.swagger.v3.parser.OpenAPIResolver.resolve(OpenAPIResolver.java:49) at io.swagger.v3.parser.OpenAPIV3Parser.readLocation(OpenAPIV3Parser.java:66) at io.swagger.parser.OpenAPIParser.readLocation(OpenAPIParser.java:16) at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:452) at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:498) at org.openapitools.codegen.cmd.Generate.run(Generate.java:415) at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:61) Caused by: java.lang.RuntimeException: Could not find ./def.yml on the classpath at io.swagger.v3.parser.util.ClasspathHelper.loadFileFromClasspath(ClasspathHelper.java:31) at io.swagger.v3.parser.util.RefUtils.readExternalRef(RefUtils.java:233)

It seems that the swagger-parser assumes that all the yaml files are in the flattened root directory (classpath), but in this case, the pets.yml is in ./pet dir and $ref def.yml also in ./pet The test case file is attached as below yaml.zip

iamchating commented 4 years ago

The same issue as #1223, but in the v2 branch, the implementation is totally different and can not be easily fixed. I have looked into the code and maybe a major change is needed.

gracekarina commented 4 years ago

Hi @iamchating, thanks for reporting this issue, the PR above was merge with a fix, please let me know it goes!.

etteeee commented 2 years ago

Hi @iamchating, thanks for reporting this issue, the PR above was merge with a fix, please let me know it goes!.

the PR only fixed v3 branch, what about v2 branch?