swagger-api / swagger-parser

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

Can't reference parent through child #1488

Open jumpman255 opened 4 years ago

jumpman255 commented 4 years ago

Here's a simplified version of my code. I have this file structure:

└─ openapi
      ├─ index.yaml
      ├─ schemas.yaml
      └─ paths.yaml

Here is index.yaml:

openapi: 3.0.0
info:
  version: 1.0.0
  title: Test API

components:
  schemas:
    $ref: './schemas.yaml'

paths:
  $ref: './paths.yaml'

Here is schemas.yaml:

User:
  description: A user of the current system
  type: object
  properties:
    name:
      type: string
  required:
    - name

Here is paths.yaml:

/user:
  get:
    summary: Get current user
    responses:
      200:
        description: OK
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'

This is the returned error from paths.yaml: MissingPointerError: Token "components" does not exist. As soon as I remove the reference, it's working.

According to the specifications:

Reference – $ref: '#/definitions/myElement' # means go to the root of the current document and then find elements definitions and myElement one after one.`

This should work.

I just made a (non)working exemple: https://github.com/jumpman255/swagger-parser-child-bug

ymohdriz commented 3 years ago

Hi @jumpman255

It's expected as the components section is not available in the current file (paths.yaml). You need to specify the file name as user schema is declared in schemas.yaml:

Use as below: $ref: './schemas.yaml#/components/schemas/User'

jumpman255 commented 3 years ago

While your solution works, mine should also work according to OpenAPI specifications but doesn't with swagger-parser. I finally resolved the refs manually with the package json-refs.

ymohdriz commented 3 years ago

@jumpman255 Can you please share the related OpenAPI specifications? Swagger-parser should adhere to spec