Laravel array validation in swagger
My form-data consists of an array 'items' which contains details like:
items[0][id]:1
items[0][amount]:50
items[1][id]:2
items[1][amount]:25
This is how it looks like in postman. How can I write the swagger comments for this structure? Below is the comment I've built but it doesn't work.
/**
* @QA\Property(
* property="items[]",
* type="array",
* @QA\Items(
* type="object",
* @QA\Property(property="id", type="string"),
* @QA\Property(property="amount", type="string"),
* ),
* ),
*/
public $items;
Let me know what needs to be done to solve this issue.
Solution 1:
OpenAPI Specification currently does not support this kind of serialization for nested objects and arrays of objects in form data. In other words, there is no syntax to define this data format.
There's a corresponding enhancement request:
https://github.com/OAI/OpenAPI-Specification/issues/1706
It discusses query parameters, but if implemented it will extend to form data too (because form data uses the same serialization methods as query parameters).
A possible workaround is to change the API to accept application/json
instead of form data and send this body as a JSON array of objects.
"items: [
{
"id": 1,
"amount": 50
},
{
"id": 2,
"amount": 25
}
]