如何在OpenAPI(Swagger)中为同一路径定义不同的查询参数?

atmip9wb  于 8个月前  发布在  其他
关注(0)|答案(4)|浏览(79)

我正在使用Swagger Codegen启动REST服务。我需要对不同的参数有不同的React。
示例:<baseURL>/path可以使用?filter1=?filter2=,这些参数应该产生不同的响应消息。
我希望我的OpenAPI YAML文件分别记录这两个查询参数。这可能吗?

9njqaruj

9njqaruj1#

它在2.0规范中不受支持,在3.x中也不受支持。
以下是OpenAPI规范库中的相应建议:
Accommodate legacy APIs by allowing query parameters in the path
Querystring in Path Specification

ddarikpa

ddarikpa2#

如果你还在找的话,我找到了解决这个问题的方法。这是一个有点黑客,但它的工作。
基本上,通过在URL中添加斜杠(/),您可以对同一路径有两个定义。
这样,您可以使用?filter1=参数为<baseURL>/path设置一个响应,并使用?filter2=参数为<baseURL>//path设置另一个响应。为每个定义给予唯一的operationId也很重要。

paths:
   /path/you/want:
      get:
         summary: Test 
         operationId: get1
         parameters:
         - name: filter1
         type: string
         in: path
         required: true
      responses:
         200:
            description: Successful response
            schema:
              $ref: '#/definitions/SomeResponse'

   /path/you//want:
     get:
         summary: Another test
         operationId: get2
         parameters:
         - name: filter2
         type: string
         in: path
         required: true
     responses:
       200:
         description: Successful response
         schema:
           $ref: '#/definitions/SomeOtherResponse'

我尝试了一个路径参数,它工作得很好!

dl5txlt9

dl5txlt93#

在swagger定义位置时,类型显式地定义了这些变量。您拥有所有必需的字段以避免变量冲突,对于json主体,您必须引用声明或使用示例模式,如下所示。对于我的例子,我使用了一个模式示例,而不是声明引用

/auth/account/password/reset/{userId}/{resetToken}:
post:
  consumes:
    - application/json
  parameters:
    - in: path
      name: userId
      type: string
      required: true
    - in: path
      type: string
      name: resetToken
      required: true
    - in: header
      name: authorization
      required: true
      type: string
    - in: body
      name: body
      required: true
      schema:
        type: object
        example:
          password: password
          confirmPassword: password
  responses:
    "200":
      description: OK
watbbzwu

watbbzwu4#

在Swagger中,您可以添加?以使端点不同。
即/articles和/articles?:

在Swagger编辑器中使用?时,您将看到错误:

然而,在您的最后一个Swagger页面上,将有标记VALID

其他信息:

记住重复条目的唯一operationId

相关问题