how to map this array json in swagger ui?

2019-09-05 17:48发布

问题:

I am working on swagger ui in php and i am trying to map json like below

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

I tried it from swagger ui demos but unable to get mapping like above json, how can i get this json mapping, what would be the annotation for that ? please help

My swagger class is

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

    }

And my post api annotation is:

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

And by this i am getting model schema like:

But i want json schema like:

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

Please help me..

回答1:

DISCLAIMER: It's been a long time since I worked with swagger-php annotations, so I'm not sure this is the best way to do this. My expertise is more in JSON Schema, not swagger-php.

Your data structure is actually two objects. The first is an object with one property: "category". Then the value of "category" is another object with two properties: "id" and "name". I'm pretty sure you need to model these as two separate objects.

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

}

Hope that helps.