How to have multiple body parameters in OpenAPI 2.0 (Swagger 2.0)?

Viewed 156

I would like to have multiple parameters in the request body like below, but the Swagger Editor says "Multiple body parameters are not allowed". How to have a combination of both object and primitive types in a single POST body?

I am expecting the criteria object and two primitive fields (offset, limit) like below:

    {
        "criteria": {
            "key": "CBC",
            "code":
        },
        "offset": 5,
        "limit" : 5
    }

OpenAPI definition:

  "paths": {
        "/user": {
          "post": {
            "tags": [
              "user Service"
            ],
            "summary": "Returns a list of user",
            "operationId": "searchUsers",
            "produces": [
              "application/json"
            ],
            "parameters": [
              {
                "in": "body",
                "name": "criteria",
                "required": true,
                "schema": {
                  "$ref": "#/definitions/Criteria"
                }
              },
              {
                "name": "offset",
                "type": "integer",
                "description": "The offset for pagination.",
                "format": "int32",
                "default": 0,
                "minimum": 0,
                "required": false,
                "in": "body"
              },
              {
                "name": "limit",
                "type": "integer",
                "description": "The maximum number user",
                "format": "int32",
                "minimum": 1,
                "maximum": 30,
                "default": 30,
                "required": false,
                "in": "body"
              }
            ],
            "responses": {
              "200": {
                "description": "Success",
                "schema": {
                  "$ref": "#/definitions/user"
                }
              },

definitions:
"Criteria": {
      "type": "object",
      "required": [
        "key"
      ],
      "properties": {
        "key": {
          "type": "string",
          "description": "search key.",
          "pattern": "[A-Za-z0-9-]+",
          "minLength": 1,
          "maxLength": 30
        },
        "code": {
          "type": "string",
          "description": "emp code filter on.",
          "pattern": "^[A-Za-z0-9]+",
          "minLength": 0,
          "maxLength": 9
        }
       
      }
    },

How to keep both the object and primitive types?

1 Answers

You're almost there.

This part is how you define the request body in OpenAPI 2.0:

            "parameters": [
              {
                "in": "body",
                "name": "criteria",
                "required": true,
                "schema": {
                  "$ref": "#/definitions/Criteria"
                }
              },

That is, it's defined as an in: body parameter, but the schema must describe the entire request body object rather than a single field.

The correct version is:

 "paths": {
    "/user": {
      "post": {
        ...

        "parameters": [
          {
            "in": "body",
            "name": "criteria",
            "required": true,
            "schema": {
              "$ref": "#/definitions/UserSearchParams"
            }
          }
        ],
        "responses": {
          ...
        }
      }
    }
  },

  "definitions": {

    "UserSearchParams": {
      "type": "object",
      "properties": {
        "criteria": {
          "$ref": "#/definitions/Criteria"
        },
        "offset": {
          "type": "integer",
          "description": "The offset for pagination.",
          "format": "int32",
          "default": 0,
          "minimum": 0
        },
        "limit": {
          "type": "integer",
          "description": "The maximum number user",
          "format": "int32",
          "minimum": 1,
          "maximum": 30,
          "default": 30
        }
      }
    },

    "Criteria": {
      "type": "object",
      "required": [
        "key"
      ],
      "properties": {
        "key": {
          "type": "string",
          "description": "search key.",
          "pattern": "[A-Za-z0-9-]+",
          "minLength": 1,
          "maxLength": 30
        },
        "code": {
          "type": "string",
          "description": "emp code filter on.",
          "pattern": "^[A-Za-z0-9]+",
          "minLength": 0,
          "maxLength": 9
        }
      }
    },

    ...
  }
}
Related