如何在 swagger 中传递多值查询参数 [英] how to pass multi value query params in swagger
问题描述
我在 swagger.yml 中有以下服务.编写该服务以便可以多次传递 page_id.例如 /pages?page_id[]=123&page_id[]=542
I have following service in swagger.yml. The service is written so that page_id can be passed multiple times. e.g /pages?page_id[]=123&page_id[]=542
我查看了此链接 https://swagger.io/specification/ 但不明白如何更新yml 这样我就可以多次传递 id.
I checked this link https://swagger.io/specification/ but couldnt understand how could i update yml so i could pass id multiple times.
我知道我必须设置 collectionFormat
但不知道如何设置.
I see that i have to set collectionFormat
but dont know how.
我尝试像下面那样更新它,但没有成功https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.
I tried updating it like below but no luck https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.
它生成的 url 像 'http://localhost:0000/pages?page_id=123%2C%20542`
it generates url like 'http://localhost:0000/pages?page_id=123%2C%20542`
'/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
推荐答案
您就快到了.将参数命名为 page_id[]
,使其成为 type: array
并使用 collectionFormat: multi
:
You are almost there. Name the parameter page_id[]
, make it type: array
and use 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,它们是保留字符.
Note that the requests will be sent with the [
and ]
characters percent-encoded as %5B
and %5D
, because they are reserved characters according to RFC 3986.
http://example.com/pages?page_id%5B%5D=123&page_id%5B%5D=456
这篇关于如何在 swagger 中传递多值查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!