如何使用OpenAPI(Swagger)描述动态形式数据?

发布于 2025-01-18 01:40:07 字数 1057 浏览 3 评论 0原文

我正在尝试为此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榆西 2025-01-25 01:40:07

动态形式数据可以使用 OpenAPI 3.0 而不是OpenAPI 2.0(Swagger 2.0)来定义。 OpenAPI 2.0仅支持表单数据中的固定密钥名称。

在OpenAPI 3.0中,您可以使用带有附加Properties的架构描述动态表单数据:

openapi: 3.0.2
...
servers:
  - url: 'http://localhost:1234/api/v1'
paths:
  /Submit:
    post:
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              # Object properties correspond to form fields
              type: object
              additionalProperties:
                type: string
      responses:
        '200':
          description: OK

在Swagger UI中测试请求时,以JSON格式输入字段名称和值:

{
  "field1": "value1",
  "field2": "value2",
  "field3": "value3"
}

Swagger UI将以单个表单字段发送这些值:

“以Swagger

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:

openapi: 3.0.2
...
servers:
  - url: 'http://localhost:1234/api/v1'
paths:
  /Submit:
    post:
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              # Object properties correspond to form fields
              type: object
              additionalProperties:
                type: string
      responses:
        '200':
          description: OK

When testing the request in Swagger UI, enter the fields names and values in the JSON format:

{
  "field1": "value1",
  "field2": "value2",
  "field3": "value3"
}

Swagger UI will send these values as individual form fields:

Sending dynamic form data in Swagger UI

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文