Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subpaths in Swagger YAML declaration

I am trying to create a REST service by describing it in Swagger YAML.

The service has three paths:

  • /versions
  • /partners/{partnerId}/users/{userId}/sessions
  • /partners/{partnerId}/books/{bookId}/

My current YAML file to describe these paths looks like this:

swagger: '2.0'
info:
  version: '0.0.1'
  title: Test API
host: api.test.com
basePath: /
schemes:
  - https
consumes:
  - application/json
produces:
  - application/json
paths:
  /versions:
    post:
      responses:
        '201':
          description: Returns all versions.
        default:
          description: unexpected error
  /partners/{partnerId}/users/{userId}/sessions:
    parameters:
      - name: partnerId
        in: path
        type: integer
      - name: userId
        in: path
        type: string
    post:
      responses:
        '201':
          description: Returns a UserSession object with info about the user session.
        default:
          description: unexpected error
  /partners/{partnerId}/books/{bookId}/:
    parameters:
      - name: partnerId
        in: path
        type: integer
      - name: bookId
        in: path
        type: string
    get:
      responses:
        '200':
          description: Gets a book.
        default:
          description: unexpected error

In this YAML file the parameter "partnerId" is declared twice.

Is there a way to make "subpaths" such that I don't have to declare the /partners/{partnerId} part of the path twice?

like image 661
uldall Avatar asked May 19 '15 13:05

uldall


1 Answers

What you can do is declare the parameter at the top level, and then refer to it.

swagger: '2.0'
info:
  version: '0.0.1'
  title: Test API
host: api.test.com
basePath: /
schemes:
  - https
consumes:
  - application/json
produces:
  - application/json
parameters:
  partnerId:
    name: partnerId
    in: path
    type: integer
paths:
  /versions:
    post:
      responses:
        '201':
          description: Returns all versions.
        default:
          description: unexpected error
  /partners/{partnerId}/users/{userId}/sessions:
    parameters:
      - $ref: '#/parameters/partnerId'
      - name: userId
        in: path
        type: string
    post:
      responses:
        '201':
          description: Returns a UserSession object with info about the user session.
        default:
          description: unexpected error
  /partners/{partnerId}/books/{bookId}/:
    parameters:
      - $ref: '#/parameters/partnerId'
      - name: bookId
        in: path
        type: string
    get:
      responses:
        '200':
          description: Gets a book.
        default:
          description: unexpected error
like image 76
Ron Avatar answered Nov 02 '22 09:11

Ron