This commit is contained in:
Christopher Davis 2021-02-01 09:50:15 -06:00
parent 9299c0e52e
commit ac7e29da21
3 changed files with 13 additions and 14 deletions

View File

@ -21,18 +21,19 @@ use Symfony\Component\PropertyInfo\Type;
* Open API schemas to support poly morphism. * Open API schemas to support poly morphism.
* *
* @see https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/ * @see https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
*
* @internal * @internal
*/ */
trait ApplyOpenApiDiscriminatorTrait trait ApplyOpenApiDiscriminatorTrait
{ {
/** /**
* @param Model $model the model that's being described, This is used to pass groups and config * @param Model $model the model that's being described, This is used to pass groups and config
* down to the children models in `oneOf` * down to the children models in `oneOf`
* @param OA\Schema $schema The Open API schema to which `oneOf` and `discriminator` properties * @param OA\Schema $schema The Open API schema to which `oneOf` and `discriminator` properties
* will be added * will be added
* @param string $discriminatorProperty The property that determine which model will be unsierailized * @param string $discriminatorProperty The property that determine which model will be unsierailized
* @param array<string, string> $typeMap the map of $discriminatorProperty values to their * @param array<string, string> $typeMap the map of $discriminatorProperty values to their
* types * types
*/ */
protected function applyOpenApiDiscriminator( protected function applyOpenApiDiscriminator(
Model $model, Model $model,
@ -40,7 +41,7 @@ trait ApplyOpenApiDiscriminatorTrait
ModelRegistry $modelRegistry, ModelRegistry $modelRegistry,
string $discriminatorProperty, string $discriminatorProperty,
array $typeMap array $typeMap
) : void { ): void {
$schema->oneOf = []; $schema->oneOf = [];
$schema->discriminator = new OA\Discriminator([]); $schema->discriminator = new OA\Discriminator([]);
$schema->discriminator->propertyName = $discriminatorProperty; $schema->discriminator->propertyName = $discriminatorProperty;
@ -54,7 +55,6 @@ trait ApplyOpenApiDiscriminatorTrait
)); ));
$schema->oneOf[] = $oneOfSchema; $schema->oneOf[] = $oneOfSchema;
$schema->discriminator->mapping[$propertyValue] = clone $oneOfSchema; $schema->discriminator->mapping[$propertyValue] = clone $oneOfSchema;
} }
} }
} }

View File

@ -74,7 +74,7 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
$annotationsReader->updateDefinition($reflClass, $schema); $annotationsReader->updateDefinition($reflClass, $schema);
$discriminatorMap = $this->doctrineReader->getClassAnnotation($reflClass, DiscriminatorMap::class); $discriminatorMap = $this->doctrineReader->getClassAnnotation($reflClass, DiscriminatorMap::class);
if ($discriminatorMap && $schema->discriminator === OA\UNDEFINED) { if ($discriminatorMap && OA\UNDEFINED === $schema->discriminator) {
$this->applyOpenApiDiscriminator( $this->applyOpenApiDiscriminator(
$model, $model,
$schema, $schema,

View File

@ -11,13 +11,12 @@
namespace Nelmio\ApiDocBundle\Tests\ModelDescriber; namespace Nelmio\ApiDocBundle\Tests\ModelDescriber;
use Doctrine\Common\Annotations\AnnotationReader;
use Nelmio\ApiDocBundle\Model\Model; use Nelmio\ApiDocBundle\Model\Model;
use Nelmio\ApiDocBundle\Model\ModelRegistry; use Nelmio\ApiDocBundle\Model\ModelRegistry;
use Nelmio\ApiDocBundle\ModelDescriber\ApplyOpenApiDiscriminatorTrait; use Nelmio\ApiDocBundle\ModelDescriber\ApplyOpenApiDiscriminatorTrait;
use OpenApi\Annotations as OA; use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Type;
class ApplyOpenApiDiscriminatorTraitTest extends TestCase class ApplyOpenApiDiscriminatorTraitTest extends TestCase
{ {
@ -70,14 +69,14 @@ class ApplyOpenApiDiscriminatorTraitTest extends TestCase
); );
} }
protected function setUp() : void protected function setUp(): void
{ {
$this->schema = new OA\Schema([]); $this->schema = new OA\Schema([]);
$this->model = $this->createModel(__CLASS__); $this->model = $this->createModel(__CLASS__);
$this->modelRegistry = new ModelRegistry([], new OA\OpenApi([])); $this->modelRegistry = new ModelRegistry([], new OA\OpenApi([]));
} }
private function createModel(string $className) : Model private function createModel(string $className): Model
{ {
return new Model( return new Model(
new Type(Type::BUILTIN_TYPE_OBJECT, false, $className), new Type(Type::BUILTIN_TYPE_OBJECT, false, $className),