Swagger PHP - 如何定义嵌套属性?

10

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

我希望展示的JSON响应(简化为此问题):

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

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

模型文件中的注释如下:

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

 }
1个回答

23

我面临同样的问题,今天已经解决了!

这是针对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: 我不知道问题出在哪里,用户界面看起来很好,但在进行请求时,post 或 payload 中没有数据。

编辑2: 将 Get 转换为 Post。 使用 file_get_contents("php://input") 正常运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接