swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.38k stars 2.18k forks source link

need help with $ref #3340

Open PinkyRabbit opened 4 years ago

PinkyRabbit commented 4 years ago

swagger.yaml

swagger: '2.0'
info:
  version: 1.0.0
  title: My title
  description: My description
schemes:
  - http
  - https
host: localhost:3000
basePath: /api/v1
# format of the responses to the client (Accepts)
consumes:
  - application/json
produces:
  - application/json
paths:
  /auth/login:
    $ref: 'test.yaml#/test'

test.yaml

test:
  post:
    summary: 'test response'
    responses:
      '200':
        description: OK  description: OK

response:

 Swagger Error
Reference could not be resolved: test.yaml#/test

Jump to line 17
Details
 Object
code: "UNRESOLVABLE_REFERENCE"
message: "Reference could not be resolved: test.yaml#/test"
 path: Array [3]
error: "Cannot use 'in' operator to search for 'test' in undefined"
level: 900
type: "Swagger Error"
description: "Reference could not be resolved: test.yaml#/test"
lineNumber: 17

Where I do wrong?

dylanirlbeck commented 4 years ago

I may be wrong, but I think in your swagger.yaml file you'll want to have the value of the $ref property under /auth/login be './test.yaml#/test' instead of 'test.yaml#/test'.

So the code would look like this:

swagger: '2.0'
info:
  version: 1.0.0
  title: My title
  description: My description
schemes:
  - http
  - https
host: localhost:3000
basePath: /api/v1
# format of the responses to the client (Accepts)
consumes:
  - application/json
produces:
  - application/json
paths:
  /auth/login:
    $ref: './test.yaml#/test'

and test.yaml would remain the same. Your only issue is how you're defining the file path. I'm also assuming that test.yaml and swagger.yaml are located in the same directory.

Let me know if this helps!