swagger-api / swagger-api/swagger-parser

Ref resolver doesn't follow JSON Reference specification

Open
#1,074 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
867
Forks
560
Avg merge
2d 21h
Merged PRs (30d)
7

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with io.swagger.v3.parser.ResolverCache.loadRef(), then trace how OpenAPIV3Parser.read("src/openapi/openapi.yaml") resolves the nested references in the issue. Reproduce the supplied openapi.yaml, components/index.yaml, and components/schemas/index.yaml structure, and verify that each relative reference is resolved against its referring document rather than the root directory.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.