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.

Unable to follow/detect model reference in another file ($ref)

See original GitHub issue

Hi!

I first created this bug in swagger-codegen project ( https://github.com/swagger-api/swagger-codegen/issues/6813 ) but then I created a small project with swagger-parser and could reproduce the issue. It is definitely a bug is swagger-parser.

The problem is that swagger-parser doesn’t seems to follow $ref in other files, thus preventing the use of multi files specs. Is is supposed to be supported? Is there a problem in my spec? Is there any workarounds?

You can reproduce easily by loading this swagger.yaml file with swagger-parser. You will notice that there is no model in the POJO representation. Both files must be in the same folder.

This is swagger.yaml

swagger: '2.0'
info:
  version: '1.0.0'
  title: Swagger Petstore (Simple)
  description: A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification
  termsOfService: http://helloreverb.com/terms/
  contact:
    name: Swagger API team
    email: foo@example.com
    url: http://swagger.io
  license:
    name: MIT
    url: http://opensource.org/licenses/MIT
host: petstore.swagger.io
basePath: /api
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /pets:
    get:
      description: Returns all pets from the system that the user has access to
      operationId: findPets
      produces:
        - application/json
        - application/xml
        - text/xml
        - text/html
      parameters:
        - name: tags
          in: query
          description: tags to filter by
          required: false
          type: array
          items:
            type: string
          collectionFormat: csv
        - name: limit
          in: query
          description: maximum number of results to return
          required: false
          type: integer
          format: int32
      responses:
        '200':
          description: pet response
          schema:
            type: array
            items:
              $ref: 'definitions.yaml#/definitions/pet'
        default:
          description: unexpected error
          schema:
            $ref: 'definitions.yaml#/definitions/errorModel'
    post:
      description: Creates a new pet in the store.  Duplicates are allowed
      operationId: addPet
      produces:
        - application/json
      parameters:
        - name: pet
          in: body
          description: Pet to add to the store
          required: true
          schema:
            $ref: 'definitions.yaml#/definitions/newPet'
      responses:
        '200':
          description: pet response
          schema:
            $ref: 'definitions.yaml#/definitions/pet'
        default:
          description: unexpected error
          schema:
            $ref: 'definitions.yaml#/definitions/errorModel'
  /pets/{id}:
    get:
      description: Returns a user based on a single ID, if the user does not have access to the pet
      operationId: findPetById
      produces:
        - application/json
        - application/xml
        - text/xml
        - text/html
      parameters:
        - name: id
          in: path
          description: ID of pet to fetch
          required: true
          type: integer
          format: int64
      responses:
        '200':
          description: pet response
          schema:
            $ref: 'definitions.yaml#/definitions/pet'
        default:
          description: unexpected error
          schema:
            $ref: 'definitions.yaml#/definitions/errorModel'
    delete:
      description: deletes a single pet based on the ID supplied
      operationId: deletePet
      parameters:
        - name: id
          in: path
          description: ID of pet to delete
          required: true
          type: integer
          format: int64
      responses:
        '204':
          description: pet deleted
        default:
          description: unexpected error
          schema:
            $ref: 'definitions.yaml#/definitions/errorModel'

This is definitions.yaml

definitions:
  pet:
    type: object
    required:
      - id
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
  newPet:
    type: object
    required:
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
  errorModel:
    type: object
    required:
      - code
      - message
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JFCotecommented, Dec 4, 2017

@gracekarina Sorry for the delay, things have been crazy a little bit. So here is an update. You solution is good but I made a slight modification. In Windows, I need to put ./ instead of /. It works with the basic Pet store samples. Final code looks like this:

swagger.yaml

swagger: '2.0'
info:
  version: '1.0.0'
  title: Swagger Petstore (Simple)
  description: A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification
  termsOfService: http://helloreverb.com/terms/
  contact:
    name: Swagger API team
    email: foo@example.com
    url: http://swagger.io
  license:
    name: MIT
    url: http://opensource.org/licenses/MIT
host: petstore.swagger.io
basePath: /api
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /pets:
    get:
      description: Returns all pets from the system that the user has access to
      operationId: findPets
      produces:
        - application/json
        - application/xml
        - text/xml
        - text/html
      parameters:
        - name: tags
          in: query
          description: tags to filter by
          required: false
          type: array
          items:
            type: string
          collectionFormat: csv
        - name: limit
          in: query
          description: maximum number of results to return
          required: false
          type: integer
          format: int32
      responses:
        '200':
          description: pet response
          schema:
            type: array
            items:
              $ref: './definitions.yaml#/definitions/pet'
        default:
          description: unexpected error
          schema:
            $ref: './definitions.yaml#/definitions/errorModel'
    post:
      description: Creates a new pet in the store.  Duplicates are allowed
      operationId: addPet
      produces:
        - application/json
      parameters:
        - name: pet
          in: body
          description: Pet to add to the store
          required: true
          schema:
            $ref: './definitions.yaml#/definitions/newPet'
      responses:
        '200':
          description: pet response
          schema:
            $ref: './definitions.yaml#/definitions/pet'
        default:
          description: unexpected error
          schema:
            $ref: './definitions.yaml#/definitions/errorModel'
  /pets/{id}:
    get:
      description: Returns a user based on a single ID, if the user does not have access to the pet
      operationId: findPetById
      produces:
        - application/json
        - application/xml
        - text/xml
        - text/html
      parameters:
        - name: id
          in: path
          description: ID of pet to fetch
          required: true
          type: integer
          format: int64
      responses:
        '200':
          description: pet response
          schema:
            $ref: './definitions.yaml#/definitions/pet'
        default:
          description: unexpected error
          schema:
            $ref: './definitions.yaml#/definitions/errorModel'
    delete:
      description: deletes a single pet based on the ID supplied
      operationId: deletePet
      parameters:
        - name: id
          in: path
          description: ID of pet to delete
          required: true
          type: integer
          format: int64
      responses:
        '204':
          description: pet deleted
        default:
          description: unexpected error
          schema:
            $ref: './definitions.yaml#/definitions/errorModel'

definitions.yaml

definitions:
  pet:
    type: object
    required:
      - id
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
  newPet:
    type: object
    required:
      - name
    properties:
      id:
        type: integer
        format: int64
      name:
        type: string
      tag:
        type: string
  errorModel:
    type: object
    required:
      - code
      - message
      - pet
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string

If I change the errorModel to this (pointer to another model), I get an error:

  errorModel:
    type: object
    required:
      - code
      - message
      - pet
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
      pet:
        $ref: "#/newPet"

Error I receive:

Exception in thread "main" java.lang.RuntimeException: Could not find newPet in contents of ./definitions.yaml
        at io.swagger.parser.ResolverCache.loadRef(ResolverCache.java:134)
        at io.swagger.parser.processors.ExternalRefProcessor.processRefToExternalDefinition(ExternalRefProcessor.java:34)
        at io.swagger.parser.processors.ExternalRefProcessor.processRefProperty(ExternalRefProcessor.java:119)
        at io.swagger.parser.processors.ExternalRefProcessor.processRefToExternalDefinition(ExternalRefProcessor.java:83)
        at io.swagger.parser.processors.PropertyProcessor.processRefProperty(PropertyProcessor.java:34)
        at io.swagger.parser.processors.PropertyProcessor.processProperty(PropertyProcessor.java:21)
        at io.swagger.parser.processors.ResponseProcessor.processResponse(ResponseProcessor.java:21)
        at io.swagger.parser.processors.OperationProcessor.processOperation(OperationProcessor.java:45)
        at io.swagger.parser.processors.PathsProcessor.processPaths(PathsProcessor.java:101)
        at io.swagger.parser.SwaggerResolver.resolve(SwaggerResolver.java:50)
        at io.swagger.parser.SwaggerParser.read(SwaggerParser.java:67)
        at io.swagger.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:431)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:283)
        at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)

Is this a but or am I doing something wrong again?

Thanks!

1reaction
JFCotecommented, Nov 17, 2017

@gracekarina Oh nice! I’m pretty busy today but next week I will try it with my real spec to see if this is working.

Thank you very much for looking into this! I’ll keep you posted

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to correct a #REF! error - Microsoft Support
The #REF! error shows when a formula refers to a cell that's not valid. This happens most often when cells that were referenced...
Read more >
#REF Excel Errors - How to Find and Fix #REF Errors in Excel
Method #2. Another method is to press Ctrl + F (known as the Excel find function) and then type “#REF!” in the Find...
Read more >
How to solve the #REF! error on spreadsheets - Sheetgo Blog
REF ! Learn how to resolve the REF Google spreadsheet error when linking data across sheets, even when out of your office or...
Read more >
git pull fails "unable to resolve reference ... - Stack Overflow
Trying to git pull after deleting the first file returned fatal: update_ref failed for ref 'HEAD': cannot lock ref 'HEAD': unable to resolve...
Read more >
Loaded external reference (xref) does not display in AutoCAD
The xref file has nothing in model space, or the wrong content is on the Model tab. The drawing elements are on a...
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