【问题标题】:How to pass array of string in swagger form data field如何在 swagger 表单数据字段中传递字符串数组
【发布时间】:2021-11-11 04:19:29
【问题描述】:

我想在表单数据中传递一个数组,如下所示:

但我在 NodeJS 控制台中将整个数组作为字符串获取,如下所示:

{
  targetUniversity: "['613e3ecfefa725074cb17968', '613e3ecfefa725074cb17969']",
  targetBusinessType: "['freelancer','sw dev']",
}

swagger 文件看起来像这样,

"/announce": {
      "post": {
        "tags": ["Announcement"],
        "description": "Make an announcement",
        "parameters": [
          {
            "name": "targetUniversity",
            "in": "formData",
            "type": "array",
            "description": "University ID in array []- from DD"
          },
          {
            "name": "targetBusinessType",
            "in": "formData",
            "type": "array",
            "description": "Business type (string - name) in array []"
          }
        ],
        "produces": ["application/json"],
        "responses": {
          "201": {
            "description": "announced successfully"
          }
        }
      }
    }

我只想要数组本身,而不是字符串格式的数组。

【问题讨论】:

  • 澄清一下 - 请求正文需要作为表单数据(即application/x-www-form-urlencodedmultipart/form-data)还是作为 JSON 发送?
  • 应该只是表单数据

标签: node.js swagger swagger-ui swagger-2.0


【解决方案1】:

数组参数需要items关键字来定义数组项的类型。此外,该操作必须指定它的 MIME 类型consumes:

        "consumes": [
          "application/x-www-form-urlencoded"
        ],
        "parameters": [
          {
            "name": "targetUniversity",
            "in": "formData",
            "type": "array",
            "items": {           // <------
              "type": "string"
            },
            "description": "University ID in array []- from DD"
          },
          {
            "name": "targetBusinessType",
            "in": "formData",
            "type": "array",
            "items": {           // <------
              "type": "string"
            },
            "description": "Business type (string - name) in array []"
          }
        ],

在 Swagger UI 中,每行输入一个数组项,不带引号:

【讨论】:

    【解决方案2】:

    您可以在OAS3中指定数组如下;

    "parameters": [
        {
          "name": "targetUniversity",
          "in": "formData", -->> according to comments this should be replaced by requestBody in OAS3
          "schema": {
            "type": "array",
            "items": {
              "type": "string"
            }
        ...
    

    结果会是这样的;

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-25
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 2013-04-02
    相关资源
    最近更新 更多