Search code examples
swaggerswagger-2.0swagger-php

How to specify the default JSON body in Swagger-PHP?


I want to specify the default JSON body for a POST request in Swagger-PHP. My annotations look like this:

/**
 * Setup order
 *
 * @SWG\Post(
 *      path="/order/setup",
 *      operationId="setupOrder",
 *      tags={"Orders"},
 *      summary="Setup an order with status draft.",
 *      description="Setup an order with status draft",
 *      consumes={"application/json"},
 *      @SWG\Parameter(
 *          name="body",
 *          in="body",
 *          default="{}",
 *          description="Json order info body (customer and products info)",
 *          required=true,
 *          @SWG\Schema(type="string")
 *      ),
 *      @SWG\Response(
 *          response=200,
 *          description="successful operation"
 *       ),
 *       @SWG\Response(response=400, description="Bad request"),
 *       security={
 *           {"api_key_security_example": {}}
 *       }
 *     )
 *
 */

As you can see I'm trying to achieve the default value with default="{}", but Swagger UI ignores this value and places 'string' instead as default value:

enter image description here

How can I change the 'string' part to a default JSON object?


Solution

  • You can achieve as you expected by modifying your @SWG\Parameter().

    Example (look at example of the property):

     *   @SWG\Parameter(
     *     name="body",
     *     in="body",
     *     description="User email used to create account.",
     *     required=true,
     *     @SWG\Schema(@SWG\Property(property="email", type="string", example="[email protected]")),
     *   )
    

    This annotation will generate something like this