如何在Swagger-PHP中指定默认的JSON主体?

时间:2018-04-19 08:28:47

标签: swagger swagger-2.0 swagger-php

我想在Swagger-PHP中为POST请求指定默认的JSON主体。我的注释看起来像这样:

/**
 * 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": {}}
 *       }
 *     )
 *
 */

正如您所看到的那样,我尝试使用default="{}",来达到默认值,但Swagger UI会忽略此值并放置' string'而是默认值:

enter image description here

如何更改'字符串'部分到默认的JSON对象?

2 个答案:

答案 0 :(得分:2)

您可以通过修改@SWG\Parameter()来实现预期的目标。

示例(查看属性示例):

 *   @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@example.com")),
 *   )

This annotation will generate something like this

答案 1 :(得分:0)

您可以使用以下内容。

/**
 * 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(ref="#/definitions/testDefinitions")
 *      ),
 *      @SWG\Response(
 *          response=200,
 *          description="successful operation"
 *       ),
 *       @SWG\Response(response=400, description="Bad request"),
 *       security={
 *           {"api_key_security_example": {}}
 *       }
 *     )
 *  @SWG\Definition(
 *      definition="PlanResponse",
 *      example={
 *         "type":"string"
 *      }
 *     )
 */

谢谢,