【问题标题】:how to pass multi value query params in swagger如何在招摇中传递多值查询参数
【发布时间】:2017-11-21 19:02:25
【问题描述】:

我在 swagger.yml 中有以下服务。编写服务以便 page_id 可以多次传递。例如/pages?page_id[]=123&page_id[]=542

我检查了此链接https://swagger.io/specification/,但无法理解如何更新 yml,以便可以多次传递 id。

我看到我必须设置collectionFormat,但不知道如何设置。

我尝试像下面这样更新它,但没有运气https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md

它生成像'http://localhost:0000/pages?page_id=123%2C%20542`这样的url

 '/pages':
    get:
      tags:
        - 
      summary: get the list of pages
      operationId: getPages
      produces:
        - application/json
      parameters:
        - name: page_id
          in: query
          description: some description
          required: false
          type: string
          collectionFormat: multi
        - name: page_detail
          in: query
          description: some description
          required: false
          type: string
      responses:
        '200':
          description: OK
        '401':
          description: Authentication Failed
        '404':
          description: Not Found
        '503':
          description: Service Not Available

【问题讨论】:

    标签: ruby swagger


    【解决方案1】:

    你快到了。将参数命名为page_id[],将其命名为type: array,然后使用collectionFormat: multi

          parameters:
            - name: page_id[]
              in: query
              description: some description
              required: false
              type: array
              items:
                type: string   # or type: integer or whatever the type is 
              collectionFormat: multi
    

    请注意,发送请求时将使用[] 字符百分比编码为%5B%5D,因为根据RFC 3986,它们是保留字符。

    http://example.com/pages?page_id%5B%5D=123&page_id%5B%5D=456
    

    【讨论】:

    • 一个问题,当我尝试为 page_id 传递一个空白值时,它不会返回空白值的数据。你知道为什么吗? ?page_id[]=def&page_id[]=%22%20%22
    • @mike %22%20%22 并不是真正的“空白”值,它是 " "(引号内的空格) - 这是您的服务器期望的有效值吗?
    • 是 page_id 也可以为空,因此通过" "
    • 这可能取决于服务器的实现方式——它期望page_id[]=" " 还是page_id[]=(没有价值)?如果您编写了服务器,请查看您的代码。如果是第三方 API,请查看其文档。
    【解决方案2】:

    来自文档:

    parameters:
    - name: id
      in: path
      description: ID of pet to use
      required: true
      schema:
        type: array
        style: simple
        items:
          type: string
    

    您必须将参数定义为数组。

    【讨论】:

    • 哪些文档?不清楚你指的是哪个版本。请提供链接。
    【解决方案3】:

    如何为数组类型的查询参数添加默认值:

        parameters:
          - name: 'liabilityType[]'
            in: query
            description: liabilityType filters the servicers list according to liability types.
            required: false
            schema:
              type: array
              items:
                type: string
            collectionFormat: multi
            value:
              - CAR
              - HOUSE
    

    我附上了这张代码在 Swagger UI 中的样子的图片 [1]:https://i.stack.imgur.com/MSSaJ.png

    【讨论】:

      猜你喜欢
      • 2022-06-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      • 2016-04-09
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多