2015-05-19 54 views
4

我想通過在Swagger YAML中描述來創建REST服務。Swagger YAML聲明中的子路徑

服務有三條路徑:

  • /版本
  • /合作伙伴/ {PARTNERID} /用戶/ {用戶id} /會話
  • /合作伙伴/ {PARTNERID} /書籍/ {BOOKID}/

我目前YAML文件來描述這些路徑是這樣的:

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 

在這個YAML文件中,參數「partnerId」被聲明瞭兩次。

有沒有辦法讓「子路徑」,使我不必聲明路徑的兩部分/partners/{partnerId}

回答

10

你可以做的是在頂層聲明參數,然後引用它。

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