简体   繁体   中英

how to map this array json in swagger ui?

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM