【发布时间】:2020-01-07 20:41:22
【问题描述】:
我正在尝试使用 Swagger 记录具有对象数组参数的 API POST 调用。但是当我尝试在 Swagger UI 中对其进行测试时,explode: true 似乎在encoding:filters 中被忽略了。
这是我的代码:
openapi: 3.0.2
info:
description: >-
My API
version: 1.0.0
title: My API
tags:
- name: myApi
description: my API
paths:
/myApi/getList:
post:
tags:
- myApi
summary: gets a list
description: gets a list
requestBody:
required: true
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
sourceId:
type: integer
description: the source id
filters:
type: array
items:
$ref: '#/components/schemas/Filter'
encoding:
filters:
contentType: application/json
explode: true
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
type: string
'500':
description: error
components:
schemas:
Filter:
type: object
properties:
field:
type: string
description: the name of the field for this filter
selection:
type: array
items:
type: string
description: the name of a selected value of the filter field
required: [attribUniqueName, selection]
如果我使用作为参数 e。 g.
sourceId: 1
filters: [
{
"field": "product",
"selection": ["Prod A", "Prod B"]
},
{
"field": "country",
"selection": ["USA", "France"]
}
]
然后 Swagger UI 使用(如果我省略 URL 编码以获得更好的可读性)生成调用:
sourceId=1&filters={"field":"product","selection":["Prod A","Prod B"]},{"field":"country","selection":["USA","France"]}
我怎样才能让它生产
sourceId=1&filters={"field":"product","selection":["Prod A","Prod B"]}&filters={"field":"country","selection":["USA","France"]}
改为?
OpenAPI 3.0.2 documentation for "Encoding Object" 声明 explode 属性“如果请求正文媒体类型不是 application/x-www-form-urlencoded,则应忽略。”但是我们在这里使用 application/x-www-form-urlencoded。还是文档错误,它应该说明“当前对象的内容类型”而不是“请求正文媒体类型”?但是,我会假设得到一个真正的数组作为参数值,即。 e.
sourceId=1&filters=[{"field":"product","selection":["Prod A","Prod B"]},{"field":"country","selection":["USA","France"]}]
如果重要:我使用的是 Swagger UI 版本 3.24.3。
【问题讨论】:
标签: swagger swagger-ui swagger-3.0