Search code examples
swaggerswagger-php

Can't get array key to work with Swagger


  /**
  * @SWG\POST(
  *   path="/visa-entry/calculate",
  *   operationId="visaEntryCalculate",
  *   tags={"Visa Entry"},
  *   summary="Calculate the price for an array of visa entries",
  *   description="Calculate the price for an array of visa entries",
  *
  *   @SWG\Parameter(
  *     name="entries",
  *     in="body",
  *     description="Visa Entry IDs to calculate to total price",
  *     required=true,
  *     @SWG\Schema(
  *       type="array",
  *       @SWG\Items(type="number")
  *     ),
  *     collectionFormat="multi"
  *   ),
  *
  *   @SWG\Response(
  *     response=200,
  *     description="OK"
  *   ),
  *
  *   @SWG\Response(
  *     response=400,
  *     description="Bad request"
  *   ),
  * )
  *
  * Calculates a visa entry
  */

What I'm trying to do is to receive an Array of numbers with the key entries.

entries: [1, 2, 3]

This docblock renders the following CURL.

curl -X POST "app.test/visa-entry/calculate" -H "accept: application/json" -H "Content-Type: application/json" -H "X-CSRF-TOKEN: " -d "[0]"

How can I get it to send the array with the key entries?


Solution

  • If you don't want the client to post the array directly in the request-body, but with a key, you'll need to specify the in=body parameter as type="object" and define the array as a property of that schema.

    Like this:

    /**
     * @SWG\POST(
     *   path="/visa-entry/calculate",
     *   operationId="visaEntryCalculate",
     *   tags={"Visa Entry"},
     *   summary="Calculate the price for an array of visa entries",
     *   description="Calculate the price for an array of visa entries",
     *
     *   @SWG\Parameter(
     *     in="body",
     *     name="json",
     *     description="Visa Entry IDs to calculate to total price",
     *     required=true,
     *     @SWG\Schema(
     *       type="object",
     *       @SWG\Property(
     *         property="entries",
     *         type="array",
     *         @SWG\Items(type="number")
     *       )
     *     ),
     *     collectionFormat="multi"
     *   ),
     *
     *   @SWG\Response(
     *     response=200,
     *     description="OK"
     *   ),
     *
     *   @SWG\Response(
     *     response=400,
     *     description="Bad request"
     *   ),
     * )
     *
     * Calculates a visa entry
     */