Array of objects
You can define an array of objects for application/json media type in the OpenAPI specification file in the request and response definitions.
Inline array in the request and response body
The requestBody or response where all the fields of the request body are defined in the requestBody section.
For example,
"/users_inline": {
"post": {
"description": "Add Multiple Users",
"operationId": "users_inline",
"requestBody": {
"description": "Add Multiple Users",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
}
}
}
}
}
}
}
"responses": {
"200": {
"description": "add User Response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"id": {
"type": "string"
}
}
}
}
}
}
}
}
Inline ref array in the request and response body
The requestBody or response where the schema type is array and it contains the reference object $ref.
For example,
"post": {
"description": "Add Multiple Users",
"operationId": "users_inline_ref",
"requestBody": {
"description": "Add new Users",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/user"
}
}
}
}
}
"responses": {
"200": {
"description": "add User Response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/user"
}
}
}
}
}
}
Array in component in the request and response body
The requestBody or response where schema contains only the reference object $ref pointing to a component.
For example,
"post": {
"description": "Add Multiple Users",
"operationId": "users",
"requestBody": {
"description": "Add new Users",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/user_list_inline"
}
}
}
}
"components": {
"schemas": {
"user_list": {
"type": "array",
"items": {
"$ref": "#/components/schemas/user"
}
}
}
}
"responses": {
"200": {
"description": "add User Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/user_list"
}
}
}
}
}
"components": {
"schemas": {
"user_list": {
"type": "array",
"items": {
"$ref": "#/components/schemas/user"
}
}
}
}
Complex object in the request and response body
The requestBody or response that contains array fields along with simple fields.
For example,
"description": "Add Complex Object",
"operationId": "aad_complex_object",
"requestBody": {
"description": "Add new Users",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/complex_object"
}
}
}
}
"components": {
"schemas": {
"complex_object": {
"type": "object",
"properties": {
"sample_string": {
"type": "string"
},
"user_array": {
"type": "array",
"items": {
"$ref": "#/components/schemas/user"
}
}
}
}
}
}
"responses": {
"200": {
"description": "add User Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/complex_object"
}
}
}
}
}
"components": {
"schemas": {
"complex_object": {
"type": "object",
"properties": {
"sample_string": {
"type": "string"
},
"user_array": {
"type": "array",
"items": {
"$ref": "#/components/schemas/user"
}
}
}
}
}
}