Ref resolver doesn't follow JSON Reference specification
See original GitHub issueUsing io.swagger.codegen.v3:swagger-codegen:3.0.7 it fails to resolve $ref
aka JSON Reference. Calling new OpenAPIV3Parser().read("src/openapi/openapi.yaml")
fails with:
java.lang.RuntimeException: Unable to load RELATIVE ref: ./schemas/index.yaml path: <path>/src/openapi
The API is not a Hello World so it is split to >30 files. Directory structure snippet:
openapi
βββ components
βΒ Β βββ index.yaml
βΒ Β βββ parameters
βΒ Β βΒ Β βββ index.yaml
βΒ Β βββ responses
βΒ Β βΒ Β βββ index.yaml
βΒ Β βββ schemas
βΒ Β βββ connection.yaml
βΒ Β βββ connectiondirection.yaml
βΒ Β βββ connectors
βΒ Β βΒ Β βββ connector.yaml
β β ...
βΒ Β βΒ Β βββ impl
βΒ Β βΒ Β βββ foo.yaml
β β ...
βΒ Β βΒ Β βββ bar.yaml
βΒ Β βββ index.yaml
βΒ Β βββ info.yaml
β ...
βΒ Β βββ reports.yaml
βΒ Β βββ transaction.yaml
βΒ Β βββ workflow.yaml
βββ openapi.yaml
The issue is caused by a βnestedβ relative referencing:
/openapi.yaml
$ref: "./components/index.yaml#foo"
/components/index.yaml
foo:
$ref: "./schemas/index.yaml#bar"
/components/schemas/index.yaml
bar:
...
According to JSON Reference:
Resolution is performed relative to the referring document.
This means the ./schemas/index.yaml
reference inside /components/index.yaml
file shouldnβt be resolved against the root directory but /components
directory! On a first sight the io.swagger.v3.parser.ResolverCache.loadRef()
operates with a static parentDirectory
only.
This is a well-documented feature and different parsers work just fine.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
Top Results From Across the Web
openAPI unable to resolve ref to external file. Component ...
The workaround that some implementations use to handle $refs in any places is to pre-process the spec using a generic JSON $ref resolver...
Read more >Using $ref - Swagger
According to RFC3986, the $ref string value (JSON Reference) should contain a URI, which identifies the location of the JSON value you are...
Read more >JSON Type Definition | Ajv JSON schema validator
you cannot use standard JTD keywords there. While strictly speaking it is allowed by the specification, these keywords should be ignored inside metadata...
Read more >Structuring a complex schema β Understanding JSON ...
If a named anchor is defined that doesn't follow these naming rules, then behavior is ... The URI-references in $ref resolve against the...
Read more >jsconfig.json Reference - Visual Studio Code
File Scope - no jsconfig.json: In this mode, JavaScript files opened in Visual Studio Code are treated as independent units. As long as...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
FYI: I workaround this issue by flattening the whole directory structure a to single humongous YAML file using Node-based swagger-parser as a pre-processor in our CI pipeline.
I am trying to reference a local file Money.json in another schema provisions.json using $ref as mentioned below:
provisions.json
"responses": { "200": { "schema": { "$ref": "resources/json/Money.json#/definitions/Money" },
resources/json/Money.json
"definitions": { "Money": { "type": "object", "properties": { "amount": { "type": "number", "format": "bigdecimal", "description": "Amount" }, "currency": { "type": "string", "description": "A unique internal identifier for the currency" } } }
I want to customize the resource loading behaviour as per my requirement. For example I want to restrict the loading of remote URLs and allow local refs or interpret the ref URLs as per my application requirements. Is it possible to maybe register my custom reference resolver and loader. From the code I see RefUtils.java tries to load it but how can I customize it ?