question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ref resolver doesn't follow JSON Reference specification

See original GitHub issue

Using 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:open
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
yuri1969commented, Sep 6, 2019

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.

0reactions
apurvaSAPcommented, Sep 15, 2020

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 ?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found