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.
*
* @see https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
*
* @internal
*/
trait ApplyOpenApiDiscriminatorTrait
{
/**
* @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
* @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
*/
protected function applyOpenApiDiscriminator(
Model $model,
@ -40,7 +41,7 @@ trait ApplyOpenApiDiscriminatorTrait
ModelRegistry $modelRegistry,
string $discriminatorProperty,
array $typeMap
) : void {
): void {
$schema->oneOf = [];
$schema->discriminator = new OA\Discriminator([]);
$schema->discriminator->propertyName = $discriminatorProperty;
@ -54,7 +55,6 @@ trait ApplyOpenApiDiscriminatorTrait
));
$schema->oneOf[] = $oneOfSchema;
$schema->discriminator->mapping[$propertyValue] = clone $oneOfSchema;
}
}
}

View File

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

View File

@ -11,13 +11,12 @@
namespace Nelmio\ApiDocBundle\Tests\ModelDescriber;
use Doctrine\Common\Annotations\AnnotationReader;
use Nelmio\ApiDocBundle\Model\Model;
use Nelmio\ApiDocBundle\Model\ModelRegistry;
use Nelmio\ApiDocBundle\ModelDescriber\ApplyOpenApiDiscriminatorTrait;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Type;
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->model = $this->createModel(__CLASS__);
$this->modelRegistry = new ModelRegistry([], new OA\OpenApi([]));
}
private function createModel(string $className) : Model
private function createModel(string $className): Model
{
return new Model(
new Type(Type::BUILTIN_TYPE_OBJECT, false, $className),