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

无法获取数组密钥以使用Swagger

  •  0
  • Aris  · 技术社区  · 8 年前
      /**
      * @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
      */
    

    我要做的是用键接收一个数字数组 entries

    entries: [1, 2, 3]
    

    此docblock渲染以下卷曲。

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

    如何让它发送带有密钥的数组 条目 ?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Bob Fanger    8 年前

    如果不希望客户端直接在请求正文中发布数组,但需要使用密钥,则需要指定 in=body 参数为 type="object" 并将数组定义为该架构的属性。

    像这样:

    /**
     * @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
     */