0

我正在用 php 处理 swagger ui,我正在尝试像下面那样映射 json

{
    "category": {
    "id": 0,
    "name": "string"
  }
}

我从 swagger ui demos 中尝试过,但无法像上面的 json 那样得到映射,我怎样才能得到这个 json 映射,它的注释是什么?请帮忙

我的招摇班是

/**
     * @SWG\Definition(@SWG\Xml(name="MyFl"))
     */ 
    class MyFl
    {

        /**
     * @SWG\Property(format="int64")
     * @var int
     */
    public $id;

    /**
     * @SWG\Property(example="doggie")
     * @var string
     */
    public $name;

    /**
     * @SWG\Property(type="Category")
     */
    public $category;

    }

我的post api注释是:

/**
     * @SWG\Post(
     *   path="/docs/fl",
     *   tags={"MY"},
     *   summary="Insert fl info",
     *   operationId="FL",
     *   produces={"application/xml","application/json"},
     *   @SWG\Parameter(in="body",name="body",description="Insert fl info",required=true,@SWG\Schema(ref="#/definitions/MyFl")
     *   ),
     *   @SWG\Response(response="default", description="successful operation")
     * )
     */

通过这个我得到模型模式,如:

在此处输入图像描述

在此处输入图像描述

但我想要 json 模式,如:

 {
        "category": {
        "id": 0,
        "name": "string"
      }
    }

请帮我..

4

1 回答 1

1

免责声明:我已经很久没有使用 swagger-php 注释了,所以我不确定这是不是最好的方法。我的专长更多是 JSON Schema,而不是 swagger-php。

您的数据结构实际上是两个对象。第一个是具有一个属性的对象:“类别”。那么“类别”的值是另一个具有两个属性的对象:“id”和“name”。我很确定您需要将它们建模为两个单独的对象。

/**
 * @SWG\Definition(@SWG\Xml(name="MyFl"))
 */ 
class MyFl
{

    /**
     * @SWG\Property(type=Category)  <-- Don't know if this is the right way to reference another schema.  You will have to check the documentation.
     */
    public $category;

}

/**
 * @SWG\Definition(@SWG\Xml(name="Category"))
 */ 
class Category
{

    /**
     * @SWG\Property(format="int64")
     * @var int
     */
    public $id;

    /**
     * @SWG\Property(example="doggie")
     * @var string
     */
    public $name;

}

希望有帮助。

于 2016-08-05T06:08:53.113 回答