[OAS3] Schemas with external references fail to resolve properly
See original GitHub issue-
swagger-parser version:
io.swagger.parser.v3:swagger-parser-v3:2.0.3-SNAPSHOTlatest from Maven Central. -
Code snippet used to load OpenAPI definition:
OpenAPI oai = new OpenAPIV3Parser().read("/path/to/index.yaml");
The attached tarball is a minimal sample which exhibits the issue:
index.yamluses external$refs to refer to a response, (components/response-with-reference.yaml)- this in turn refers to a schema (
components/schema-with-reference.yaml) - this in turn refers to another schema to define the type of items in an array (
components/referent.yaml)
On running the code and examining the toString() output of the OpenAPI object obtained (see attached), the $ref for items becomes ./components/components/referent.yaml, which is incorrect. This appears to be due to the invocation of constructRef in ExternalRefProcessor#processRefSchema.
Attempting to run openapi-generator-cli with this definition results in the error
java.lang.RuntimeException: Could not find ./components/components/referent.yaml on the classpath
...
[main] WARN io.swagger.v3.parser.OpenAPIV3Parser - Exception while reading:
java.lang.RuntimeException: Unable to load RELATIVE ref: ./components/components/referent.yaml
Finally, we would like to understand which path is used to process relative references, is it the root location (the location containing the index.yaml), or is it the location containing the current document?
- The swagger.io documentation states that “Remote Reference…Uses the whole document located on the same server and in the same location”. The phrase “same location” is a little unclear here but my interpretation is that it refers to the “current” file, eg the one containing the
$ref. - The OpenAPI 3 Spec states that “Relative references used in $ref are processed…using the URL of the current document as the base URI”.
If the spec means that the current document should be used as the base URI, then why does the $ref in components/response-with-reference.yaml have to be ./components/schema-with-reference.yaml? Surely it should just be schema-with-reference.yaml since components is already on the “current path”?
You can surely see that there is an inconsistency between the way the reference is made from response-with-reference.yaml and schema-with-reference.yaml, in that to currently get generation to work, one has to contain components and the other not.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (6 by maintainers)

Top Related StackOverflow Question
@webron that makes perfect sense to me but that doesn’t seem to be happening in practice.
If you look at the attached tar you will see that
response-with-reference.yamlhas the contents:This correctly resolves
./components/schema-with-reference.yamldespite the fact thatresponse-with-reference.yamlis also in thecomponentsdirectory. If your assertion were correct then the$refline should bebut this fails.
HiI have create this new issue need to write a java code for validating a json response against a OpenApi json schema · Issue #1126 · swagger-api/swagger-parser
| | | | | |
|
| | | | need to write a java code for validating a json response against a OpenA…
need to write a java code for validating a json response against a OpenApi json schema. For example the petstore… |
|
|
your help is much appreciated in this. thanks. On Friday, June 14, 2019, 06:30:48 PM GMT+1, Grace Karina Gonzalez Diaz notifications@github.com wrote:
@sivagithub2019 can you please add a new issue, and please add an example so we can discuss, this is a closed issue, so It will be hard to follow in here. thanks!
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.