Search code examples
swaggerpostmanopenapipostman-collection-runnerpostman-pre-request-script

Postman: "Invalid data type. Must be either, array, boolean, number" error


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?


Solution

  • 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"
                        }
                      }
                    }
                  }
                }
              }
            },
            ...
    
          }
        }
      }
    }