ruby 如何在swagger中传递多值查询参数

0mkxixxg  于 2023-05-06  发布在  Ruby
关注(0)|答案(3)|浏览(179)

我在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
pdkcd3nj

pdkcd3nj1#

你就快成功了。将参数命名为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
s8vozzvw

s8vozzvw2#

从文档:

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

必须将参数定义为数组。

nle07wnf

nle07wnf3#

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

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

我附上了一张图片,展示了这段代码在SwaggerUI中的样子

相关问题