代码之家  ›  专栏  ›  技术社区  ›  Paul

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

  •  0
  • Paul  · 技术社区  · 6 年前

    我想在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 回复  |  直到 6 年前
        1
  •  5
  •   Pusparaj    6 年前

    通过修改 @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

        2
  •  0
  •   Pratik Mehta    6 年前

    您可以像下面这样使用。

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

    谢谢