【问题标题】:How to send a JSON array in multipart/form-data in Swagger UI?如何在 Swagger UI 中的 multipart/form-data 中发送 JSON 数组?
【发布时间】:2021-09-04 10:56:52
【问题描述】:

我正在尝试通过multipart/form-data 发送一个对象数组:

post:
      summary: Creates a user
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties: # Request parts
                id:
                  type: string
                  format: uuid
                address:      # <---------
                  type: array
                  items:
                    type: object
                    properties:
                      street:
                        type: string
                      city:
                        type: string
                profileImage:
                  type: string
                  format: base64

但是 Swagger UI 错误地发送了 address 数组 - 作为 {},{} 而不是 [{},{}],也就是说,没有封闭的方括号:

我什至尝试将其单独编码为 JSON。

请问我错过了什么?

【问题讨论】:

  • 请澄清您所说的“未正确发送”是什么意思。您期望的结果是什么?实际会发生什么?
  • 数组尖括号被删除。也就是说,发送的是这个“{},{}”而不是这个“[{},{}]”
  • here 是结果图片

标签: multipartform-data swagger-ui openapi


【解决方案1】:

我后来通过添加示例使其工作

post:
      summary: Creates a user
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties: # Request parts
                id:
                  type: string
                  format: uuid
                address:
                  type: array
                  items:
                    type: object
                    properties:
                      street:
                        type: string
                      city:
                        type: string
                    example:
                      - street: Jones Street, Manhattan
                        city: New York
                      - street: Hollywood Boulevard
                        city: Los Angeles
                profileImage:
                  type: string
                  format: base64

我的观察只是发送或修改示例中已有的内容,添加新内容的格式将不正确。也就是说,如果您在数组中有两个项目,则使用这两个项目不要添加额外的项目

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 2020-11-08
    • 1970-01-01
    • 2018-04-08
    • 2019-04-14
    • 1970-01-01
    相关资源
    最近更新 更多