Swagger YAML 声明中的子路径

Subpaths in Swagger YAML declaration

我正在尝试通过在 Swagger YAML 中描述来创建 REST 服务。

该服务具有三个路径:

我当前用于描述这些路径的 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" 被声明了两次。

有没有办法让 "subpaths" 不必两次声明路径的 /partners/{partnerId} 部分?

你可以做的是在顶层声明参数,然后引用它。

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