Nelmioapidocbundle: Extended property of model

Created on 7 May 2018  路  2Comments  路  Source: nelmio/NelmioApiDocBundle

Hello,

I have some issues extending one property of my model in the response. I did some digging into issues here and in swagger repo but didn't succeed to find anything relevant.

I'm trying to create a PaginatedResult with a "results" array, containing arbitrary objects. I tried to take some inspiration here but didn't manage to do it :
https://github.com/zircote/swagger-php/blob/master/Examples/dynamic-reference/api-spec.php

    /**
     * @Operation(
     *    [...]
     *     @SWG\Response(
     *         response="200",
     *         description="OK",
     *         @SWG\Schema(
     *             ref=@Model(type=PaginatedResult::class)),
     *             @SWG\Property(
     *                  property="results",
     *                  type="array",
     *                  @SWG\Items(ref=@Model(type=Product::class))
     *             )
     *         )
     *     )
     *  )
     * @Rest\View()
     * @Rest\Get("/search")
     **/
     public function getProductsAction(Request $r) {
           [...]
     }

with a PaginatedResult object as this :

<?php

namespace AppBundle\DTO;

use JMS\Serializer\Annotation as JMS;

class PaginatedResult
{
    /**
     * @JMS\SerializedName("count")
     * @JMS\Type("integer")
     */
    private $count;

    /**
     * @JMS\SerializedName("offset")
     * @JMS\Type("integer")
     */
    private $offset;

    /**
     * @JMS\SerializedName("limit")
     * @JMS\Type("integer")
     */
    private $limit;

    /**
     * @JMS\SerializedName("results")
     * @JMS\Type("array")
     */
    private $results;

    [...] getters and setters [...]
}

Model in UI :
image
Sample value :
image

Am i doing it wrong ?

Ty for the support

Most helpful comment

Hello @GuilhemN

Sorry for the late response and thank you for your support, i had to adapt it since "[" and "]" caused a syntax error in my environment.

The final code is this, in case some people are in the same situation :

    /**
     * @Operation(
     *    [...]
     *     @SWG\Response(
     *         response="200",
     *         description="OK",
     *         @SWG\Schema(
     *              allOf={
     *                 @SWG\Schema(ref=@Model(type=PaginatedResult::class)),
     *                 @SWG\Schema(type="object",
     *                     @SWG\Property(
     *                          property="results",
     *                          type="array",
     *                          @SWG\Items(ref=@Model(type=Product::class))
     *                      )
     *                 )
     *             }
     *         )
     *     )
     *  )
     * @Rest\View()
     * @Rest\Get("/search")
     **/
     public function getProductsAction(Request $r) {
           [...]
     }

I'll close the issue now :)

All 2 comments

I think what you're looking for is @Schema::$allOf:
```php
/*
* @Operation(
* [...]
* @SWGResponse(
* response="200",
* description="OK",
* @SWG\Schema(
* allOf=[
* @SWG\Schema(ref=@Model(type=PaginatedResult::class))),
* @SWG\Schema(type="object",
* @SWG\Property(
* property="results",
* type="array",
* @SWG\Items(ref=@Model(type=Product::class))
* )
* )
* ]
* )
* )
* )
* @Rest\View()
* @Rest\Get("/search")
*
/
public function getProductsAction(Request $r) {
[...]
}

Hello @GuilhemN

Sorry for the late response and thank you for your support, i had to adapt it since "[" and "]" caused a syntax error in my environment.

The final code is this, in case some people are in the same situation :

    /**
     * @Operation(
     *    [...]
     *     @SWG\Response(
     *         response="200",
     *         description="OK",
     *         @SWG\Schema(
     *              allOf={
     *                 @SWG\Schema(ref=@Model(type=PaginatedResult::class)),
     *                 @SWG\Schema(type="object",
     *                     @SWG\Property(
     *                          property="results",
     *                          type="array",
     *                          @SWG\Items(ref=@Model(type=Product::class))
     *                      )
     *                 )
     *             }
     *         )
     *     )
     *  )
     * @Rest\View()
     * @Rest\Get("/search")
     **/
     public function getProductsAction(Request $r) {
           [...]
     }

I'll close the issue now :)

Was this page helpful?
0 / 5 - 0 ratings