1

I get the following error when creating API in Postman: "Invalid data type. Must be either, array, boolean, integer, number, object or string"

The error is fixed when converting the line "type": "file" to "type": "object", but I am not sure if there is a proper way for this situation. Because with this update, the request may not be passed when sending request in Postman.

"parameters": [
  {
      "in": "body",
      "name": "file",
      "description": "file",
      "required": false,
      "schema": {
          "type": "array",
          "items": {
              "type": "file"
          }
      }
  },
  ...
]

So, how can I fix the problem in Postman?

Helen
  • 87,344
  • 17
  • 243
  • 314
Jack
  • 1
  • 21
  • 118
  • 236

1 Answers1

2

The problem is not with Postman, but with the API definition. "type": "file" is not a valid type value for use in schemas and body parameters, this type can only be used in in: formData parameters.

On the other hand, I do not add file while trying to test my app via Postman. For this reason, I just want to suppress the errors

In this case you could change "type": "file" to "type": "string" to suppress import errors. Or remove the entire problematic operation from the API definition.


How to properly define file uploads in OpenAPI

The API definition is trying to describe uploading of a file array - but this is not supported in OpenAPI 2.0 (swagger: '2.0'). OAS2 only supports upload of individual named files via multipart/form-data, in which case the API definition would look like this:

{
  "swagger: "2.0",
  ...

  "paths": {
    "/something": {
      "post": {
        "consumes": [
          "multipart/form-data"
        ],
        "parameters": [
          {
            "in": "formData",
            "name": "file1",
            "type": "file"    // A single file sent via form field "file1"
          },
          {
            "in": "formData",
            "name": "file2",
            "type": "file"    // Another file sent via form field "file2"
          }
        ]
        ...
}

Uploading an array of files is supported in OpenAPI 3 though:

{
  "openapi": "3.0.0",
  ...

  "paths": {
    "/something": {
      "post": {
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "file": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "format": "binary"
                    }
                  }
                }
              }
            }
          }
        },
        ...

      }
    }
  }
}
Helen
  • 87,344
  • 17
  • 243
  • 314
  • Thank you very much these perfect explanations, voted up. In this scene, should I use `object` or `text` instead of `file` value? Because I try to update `"application/json"` to `"multipart/form-data"` in the `consumes` section, but the problem is not fixed. – Jack Jul 09 '21 at 11:08
  • You need to change not only `consumes` but also replace the `"in": "body"` parameter with one or more `"in": "formData"` parameters, as in my first example. But it probably won't achieve the desired result anyway because it's not an array of files but N separate file parameters. OpenAPI 2 just does not support file arrays, at all. The proper solution is to convert your OpenAPI 2.0 file to OpenAPI 3.0, fix the `requestBody` definition as in my 2nd example, and use the fixed OpenAPI 3.0 file with Postman instead. – Helen Jul 09 '21 at 11:24
  • ACtually there is no chance for me to use OpenAPI **v3.0** and I have to continue to use OpenAPI **v2.0**. On the other hand, I do not add file while trying to test my app via Postman. For this reason, I just want to suppress the errors and wondering what is the most proper way. In this scene, what would you suggest? – Jack Jul 09 '21 at 11:48
  • In that case you could change `"type": "file"` to `"type": "string"`, or remove the problematic operation entirely. – Helen Jul 09 '21 at 12:31