如何使用OpenAPI(Swagger)描述动态表单数据? [英] How to describe dynamic form data using OpenAPI (Swagger)?

查看:21
本文介绍了如何使用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
  ...

我的接口定义如下:

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

是否可以定义具有不同键和值的动态FormData?

推荐答案

动态表单数据可以使用OpenAPI 3.0定义,但不能使用OpenAPI 2.0(Swagger 2.0)定义。OpenAPI 2.0仅支持表单数据中的固定密钥名称。

在OpenAPI 3.0中,您可以使用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


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

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

Swagger UI将这些值作为单个表单域发送:

这篇关于如何使用OpenAPI(Swagger)描述动态表单数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆