【问题标题】:How to specify the default JSON body in Swagger-PHP?如何在 Swagger-PHP 中指定默认的 JSON 正文?
【发布时间】:2018-07-11 17:03:17
【问题描述】:

我想在 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 会忽略此值并将“字符串”作为默认值:

如何将“字符串”部分更改为默认 JSON 对象?

【问题讨论】:

    标签: swagger swagger-2.0 swagger-php


    【解决方案1】:

    您可以通过修改您的@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")),
     *   )
    

    【讨论】:

      【解决方案2】:

      你可以像下面这样使用。

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

      谢谢,

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-07
        • 1970-01-01
        • 2015-01-11
        • 2021-03-28
        • 2021-09-02
        相关资源
        最近更新 更多