2021-02-01 08:37:20 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle package.
|
|
|
|
*
|
|
|
|
* (c) Nelmio
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Nelmio\ApiDocBundle\ModelDescriber;
|
|
|
|
|
|
|
|
use Nelmio\ApiDocBundle\Model\Model;
|
|
|
|
use Nelmio\ApiDocBundle\Model\ModelRegistry;
|
2022-04-30 20:06:37 +02:00
|
|
|
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
|
2021-02-01 08:37:20 -06:00
|
|
|
use OpenApi\Annotations as OA;
|
|
|
|
use Symfony\Component\PropertyInfo\Type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains helper methods that add `discriminator` and `oneOf` values to
|
|
|
|
* Open API schemas to support poly morphism.
|
|
|
|
*
|
|
|
|
* @see https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
|
2021-02-01 09:50:15 -06:00
|
|
|
*
|
2021-02-01 08:37:20 -06:00
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
trait ApplyOpenApiDiscriminatorTrait
|
|
|
|
{
|
|
|
|
/**
|
2021-02-01 09:50:15 -06:00
|
|
|
* @param Model $model the model that's being described, This is used to pass groups and config
|
|
|
|
* down to the children models in `oneOf`
|
|
|
|
* @param OA\Schema $schema The Open API schema to which `oneOf` and `discriminator` properties
|
|
|
|
* will be added
|
|
|
|
* @param string $discriminatorProperty The property that determine which model will be unsierailized
|
|
|
|
* @param array<string, string> $typeMap the map of $discriminatorProperty values to their
|
|
|
|
* types
|
2021-02-01 08:37:20 -06:00
|
|
|
*/
|
|
|
|
protected function applyOpenApiDiscriminator(
|
|
|
|
Model $model,
|
|
|
|
OA\Schema $schema,
|
|
|
|
ModelRegistry $modelRegistry,
|
|
|
|
string $discriminatorProperty,
|
|
|
|
array $typeMap
|
2021-02-01 09:50:15 -06:00
|
|
|
): void {
|
2021-02-01 08:37:20 -06:00
|
|
|
$schema->oneOf = [];
|
2022-04-30 20:06:37 +02:00
|
|
|
$discriminator = Util::getChild($schema, OA\Discriminator::class);
|
|
|
|
$discriminator->propertyName = $discriminatorProperty;
|
|
|
|
$discriminator->mapping = [];
|
2021-02-01 08:37:20 -06:00
|
|
|
foreach ($typeMap as $propertyValue => $className) {
|
2022-04-30 20:06:37 +02:00
|
|
|
$oneOfSchema = Util::createChild($schema, OA\Schema::class);
|
2021-02-01 08:37:20 -06:00
|
|
|
$oneOfSchema->ref = $modelRegistry->register(new Model(
|
|
|
|
new Type(Type::BUILTIN_TYPE_OBJECT, false, $className),
|
|
|
|
$model->getGroups(),
|
|
|
|
$model->getOptions()
|
|
|
|
));
|
|
|
|
$schema->oneOf[] = $oneOfSchema;
|
2022-04-30 20:06:37 +02:00
|
|
|
$discriminator->mapping[$propertyValue] = $oneOfSchema->ref;
|
2021-02-01 08:37:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|