9

我正在使用 Swagger PHP,并且大多数定义都很容易定义,但是我遇到了一个特定数据的问题,该数据不是单独类的一部分,而是关联数组的一部分。

我希望显示的 json 响应(针对这个问题进行了简化):

{
"id": 1,
"status": "published",
"gps": {
    "lat": "0.00000000",
    "lng": "0.00000000"
}

和很容易定义,但是id是一个问题,因为没有单独的类来定义它,它是模型内部的一个数组。是否可以在不必创建虚拟类的情况下定义此数组?statusgps

目前模型文件中的注释:

/**
 * @SWG\Definition(@SWG\Xml(name="Event"))
 */
 class Event extends BaseModel {
     /**
     * @SWG\Property(
     *      property="id",
     *      type="integer",
     *      example="103"
     * )
     * @SWG\Property(
     *      property="status",
     *      type="string",
     *      enum={"published", "draft", "suspended"}
     *      example="published"
     * )
     */

 }
4

1 回答 1

20

遇到了同样的问题,今天解决了!

这是针对Swagger 2.0

以下是我用来实现嵌套参数的注释嵌套..

/**
 * @SWG\Post(
 *   path="/getCustomerByEmail.php",
 *   summary="List the details of customer by the email.",
 *   consumes={"string"},
 *   produces={"application/json"},
 *   @SWG\Parameter(
 *     name="email",
 *     in="body",
 *     description="Customer email to ge the data",
 *     required=true,
 *     @SWG\Schema(
 *       @SWG\Property(
 *         property="id",
 *         type="object",
 *         @SWG\Property(
 *           property="abc",
 *           type="object",
 *           @SWG\Property(
 *             property="inner abc",
 *             type="number",
 *             default=1,
 *             example=123
 *           )
 *         ),
 *         @SWG\Property(
 *           property="xyz",
 *           type="string",
 *           default="xyz default value",
 *           example="xyz example value",
 *         )
 *       )
 *     )
 *   ),
 *   @SWG\Response(
 *     response=200,
 *     description="Details of the customer"
 *   ),
 *   @SWG\Response(
 *     response=400,
 *     description="Email required"
 *   ),
 *   @SWG\Response(
 *     response=404,
 *     description="Customer does not exist"
 *   ),
 *   @SWG\Response(
 *     response="default",
 *     description="an ""unexpected"" error"
 *   )
 * )
 */
/**

输出如下

注意:我正在开发一个需要原始 PHP 但仍想使用 Swagger 的项目。因此,我没有创建模型,而是使用这种技术来制作嵌套参数。


编辑 1:我不知道问题出在哪里,UI 符合预期,但在发出请求时,帖子或有效负载中没有数据。

编辑 2:将获取转换为发布。与file_get_contents("php://input")

于 2016-08-17T06:13:41.133 回答