1

I want to include Array of objects in api/doc view. But I am unable to get that.

From the code below:

@SWG\Schema(        
  @SWG\Property(property="project-name", type="string"),    
  @SWG\Property(property="project-detail", type="array",
    @SWG\Items(type="object",
        @SWG\Property(property="name", type="string", ),
        @SWG\Property(property="category", type="string",),
    ),
    @SWG\Items(type="object",
        @SWG\Property(property="new_name", type="string", ),
        @SWG\Property(property="new_category", type="string",),
    ),
  ),
),

I get the output:

{
  "project-name": "string",
  "project-detail": [
     {
       "name": "string",
       "category": "string"
     }
   ]
}

But I want below one:

{
  "project-name": "string",
  "project-detail": [
     {
       "name": "string",
       "category": "string"
     },
     {
       "new_name": "string",
       "new_category": "string"
     },
   ]
}

Please help me out for this.

Rahul G.
  • 21
  • 1
  • 5
  • Swagger 2 does not support compound types (`Type1|Type2`), this was only added to OpenApi 3 (see https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#oneof). Unfortunately to use it you have to upgrade to NelmioApiDocBundle v4 (see https://github.com/nelmio/NelmioApiDocBundle/blob/master/UPGRADE-4.0.md#upgrading-from-3x-to-40). – Guilhem N Aug 15 '20 at 15:56
  • Put it in example in project detail level property ex. example="{{}, {}, {}, {}}" – Michael Mendoza Oct 13 '21 at 15:03

2 Answers2

2

You can only display one object on the array of objects. Because Swagger has been created to explain what you have to give to the API and what is returned (not required).

When you're looking an array and an object inside, of course you're understanding this isn't an array with 1 object.

Hope it helps !

Lord'

0

Modify your project detail property by adding example ex.

example="{
         {
          "name": "String",
          "category": "String"
         }, 
         {
          "name": "String",
          "category": "String"
         }
        }"
Michael Mendoza
  • 91
  • 1
  • 2
  • 10