2

I use Swagger and JMS serializer and show built OpenAPI JSON using NelmioAPIDocBundle https://github.com/nelmio/NelmioApiDocBundle

I have an object with a property, which is an array, that might include several different type of another objects that have their own common properties but one property is different for all of that objects. I do as you can see below and OpeAPI JSON is built, but NelmioAPIDocBundle does not show that model correctly, it shows just NULL instead.

Could you please explain what wrong I do? Is there a way to do what I need?

Thanks in advance!

<?php

use Swagger\Annotations as SWG;
use Nelmio\ApiDocBundle\Annotation\Model;

class A
{
    /**
     * @SWG\Property(
     *     type="array",
     *     @SWG\Items(ref=@SWG\Schema(allOf={
     *                  @SWG\Schema(ref=@Model(type=TypeOne::class)),
     *                  @SWG\Schema(ref=@Model(type=TypeTwo::class)),
     *              }
     *          )
     *      )
     * )
     */
    protected $objects;
}
ambrous
  • 194
  • 2
  • 11
  • You probably need to use Swagger-PHP **v.3** and `@OA` (OpenAPI 3.0) annotations instead of `@SWG` (OpenAPI 2.0). Something like `@OA\Items(oneOf={@OA\Schema(ref=...), @OA\Schema(ref=...),})` – Helen Sep 27 '19 at 08:01
  • Helen, thanks a lot, I will tri it. – ambrous Sep 27 '19 at 12:46
  • It turned out that NelmioApiDocBundle does not support Swagger-PHP v3, but it reallt works with v3. Moreover, NelmioApiDoc supports only ```allOf``` composition. By the way I have found a mistake in my annotation and now it works at least with ```allOf```: `/** * @SWG\Property( * type="array", * @SWG\Items(allOf={ * @SWG\Schema(ref=@Model(type=TypeOne::class)), * @SWG\Schema(ref=@Model(type=TypeTwo::class)), * } * ) * ) */ ` – ambrous Sep 27 '19 at 15:09
  • `allOf` is simply wrong here, it should be `oneOf` (if it's supported). But maybe this can be solved differently, using [model inheritance/composition/polymorphism](https://stackoverflow.com/a/35916573/113116). If you post your `TypeOne` and `TypeTwo` classes and show their common properties and different properties, maybe we can come up with a proper workaround. – Helen Sep 27 '19 at 15:31
  • I agree with you. I will check whether we can polymorphism here. Very appreciate for your answer! – ambrous Sep 27 '19 at 15:55
  • I've managed to do similar for property too `@OA\Property( oneOf={ @Model, ...` but even though it works, it shows the whole namespace - but if wrapped in `@OA\Schema` it works as expected (just class name :-) ) – jave.web Jun 01 '22 at 14:01

0 Answers0