0

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:

enter image description here

enter image description here

But i want json schema like:

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

Please help me..

1
  • You should post what you have tried so far. Then we can help you get to the right answer. That way you will understand the answer better than if you are just given the answer. Commented Aug 5, 2016 at 2:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That makes sense if I guessed the syntax wrong for @SWG\Property(type=Category) (like I commented). I think if you figure out the correct syntax for referencing Category, it might work. Is there a ref attribute maybe? Something like @SWG\Property(ref="#/definitions/Category")?
can you give some reference material for this?
please help me check the problem here stackoverflow.com/questions/39228284/… it is related to same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.