有没有任何可能的方法可以在参数变化的情况下多次定义相同的路径



我需要定义一个在参数更改的情况下多次使用的服务。我需要多次定义它,但每当我第二次定义它时,它就会自动覆盖上一次。以下是我对它的定义。这是第一个定义:

  /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规范中多次出现相同的路径?

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

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

这个问题是OpenAPI的Github上的一个公开问题(标记为182)。

我希望这能有所帮助!

最新更新