2

我需要定义一个多次使用并更改参数的服务。我需要多次定义它,但每当我第二次定义它时,它会自动覆盖前一个。以下是我如何定义它。这是第一个定义:

  /sevice :
    post:
      summary: Service ABC
      description: |
        This service is used to get ABC.
      parameters:
        - name: XYZ
          in: query
          description: '8'
          required: true
          type: number
          format: integer
        - name: LMN
          in: query
          description: '2'
          required: true
          type: number
          format: integer
      tags:
        - ABC
      responses:
        '200':
          description: An array of products
          schema:
            type: array
            items:
              $ref: '#/definitions/ABC'
        default:
          description: Unexpected error
          schema:
            $ref: '#/definitions/Error'

这是第二个定义:

  /sevice :
    post:
      summary: Service ABC
      description: |
        This service is used to remove ABC.
      parameters:
        - in: body
          name: jsonData
          description: Object that needs to be sended to the store.
          required: true
          schema:
            $ref: '#/definitions/PQR'
      tags:
        - ABC
      responses:
        '200':
          description: An array of products
          schema:
            type: array
            items:
              $ref: '#/definitions/LMN'
        default:
          description: Unexpected error
          schema:
            $ref: '#/definitions/Error'

是否可以在一个 API 规范中多次出现相同的路径?

4

1 回答 1

2

不,这是不可能的。我尝试这样做,但在使用Swagger Editor检查时会出现重复错误,因为我的 API 是基于 Swagger 定义构建的。我的建议是为该服务的目的附加一个名称的路径,例如定义一个 /service/get 或 /service/delete。

另外,我看到您的第二个定义旨在删除资源,而您的第一个定义是获取资源。您可以使用第二个 DELETE HTTP 方法和第一个 GET HTTP 方法并将这两个定义组合在一起。这样,您的路径是相同的,并且可以执行不同的 HTTP 功能。此链接是您可以使用的所有 HTTP 方法选项的列表。POST 通常用于创建全新的资源。

这个问题是 OpenAPI 的 Github 上的一个未解决问题(标记为 182)。

我希望这有帮助!

于 2016-07-15T18:39:51.123 回答