如何使用OpenAPI(Swagger)描述动态形式数据?
我正在尝试为此 multipart/form-data
请求创建 OpenAPI 定义:
curl -X POST \
http://localhost:1234/api/v1/Submit \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'sessionkey: kjYgfORsZ0GeiCls0FcR7w==' \
-F item1=abc \
-F item2=def
-F item3=ghi
...
我的 API 定义如下:
post:
consumes:
- multipart/form-data
produces:
- application/json
parameters:
- in: formData
name: item1
type: string
- in: formData
name: item2
type: string
它适用于 formData 中的固定字段。
但是,我的表单数据将是动态的,并且我需要能够发送任意键和值。
我尝试更改表单参数以使用数组和additionalProperties,但它不会产生所需的结果:
- in: formData
schema:
additionalProperties:
type: object
...
- in: formData
type: array
items:
type: string
Is it possible to Definedynamic formData with different key and value?
I'm trying to create an OpenAPI definition for this multipart/form-data
request:
curl -X POST \
http://localhost:1234/api/v1/Submit \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'sessionkey: kjYgfORsZ0GeiCls0FcR7w==' \
-F item1=abc \
-F item2=def
-F item3=ghi
...
My API definition is like this:
post:
consumes:
- multipart/form-data
produces:
- application/json
parameters:
- in: formData
name: item1
type: string
- in: formData
name: item2
type: string
It works fine with fixed fields in formData.
However, my form data will be dynamic, and I need to be able to send arbitrary keys and values.
I tried changing form parameters to use an array and additionalProperties
, but it does not produce the desired result:
- in: formData
schema:
additionalProperties:
type: object
...
- in: formData
type: array
items:
type: string
Is it possible to define dynamic formData with different keys and values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
动态形式数据可以使用 OpenAPI 3.0 而不是OpenAPI 2.0(Swagger 2.0)来定义。 OpenAPI 2.0仅支持表单数据中的固定密钥名称。
在OpenAPI 3.0中,您可以使用带有
附加Properties
的架构描述动态表单数据:在Swagger UI中测试请求时,以JSON格式输入字段名称和值:
Swagger UI将以单个表单字段发送这些值:
Dynamic form data can be defined using OpenAPI 3.0 but not OpenAPI 2.0 (Swagger 2.0). OpenAPI 2.0 only supports fixed key names in form data.
In OpenAPI 3.0, you can describe dynamic form data using a schema with
additionalProperties
:When testing the request in Swagger UI, enter the fields names and values in the JSON format:
Swagger UI will send these values as individual form fields: