Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger PHP - Describe array of objects

I use Laravel + Swagger PHP. In one of my services, I handle parameters like this :

$params = [
    'addressChild' => [
        'address' => ' ... ',
        'city' => ' ... ',
    ],
    'addressParent' => [
        'address' => ' ... ',
        'city' => ' ... ',
    ],
];

I am trying to describe this schema in Swagger Annotations, using OpenApi 3 :

/**
 * @OA\Post(
 *      path="/entity/{entity}/addresses",
 *      @OA\Response(
 *          response=200,
 *          @OA\JsonContent()
 *      ),
 *      @OA\RequestBody(
 *          description="Addresses to store",
 *          required=true,
 *          @OA\JsonContent(
 *              type="object",
 *              @OA\Property()
 *          ),
 *      ),
 * )
 *
 */
public function update(Request $request)
{
    // ...
}

I tried many things like :

 @OA\JsonContent(
     type="object",
     @OA\Property(name="addressKid", ref="#/components/schemas/ParkingAddressRequest") // error
 ),

My address data is described in ref="#/components/schemas/ParkingAddressRequest" schema, how can I set it as an array in RequestBody ?

like image 244
Vincent Decaux Avatar asked Oct 24 '25 07:10

Vincent Decaux


1 Answers

If I understood your problem, you could do it like this

/**
 * @OA\Post(
 *      path="/entity/{entity}/addresses",
 *      @OA\Response(
 *          response=200,
 *          @OA\JsonContent()
 *      ),
 *      @OA\RequestBody(
 *          description="Addresses to store",
 *          required=true,
 *          @OA\JsonContent(
 *              type="array",
 *              @OA\Items(ref="#/components/schemas/ParkingAddressRequest")
 *          ),
 *      ),
 * )
 *
 */
like image 92
Patric Robert Gutersohn Avatar answered Oct 27 '25 02:10

Patric Robert Gutersohn