Skip to content

[Serializer] test: discriminator without matching property #50472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Serializer\Annotation\DiscriminatorMap;

#[DiscriminatorMap(typeProperty: 'discr', mapping: ['concrete' => ConcreteWithDiscriminator::class])]
abstract class AbstractWithDiscriminator
{
/**
* @var int The id
*/
public ?int $id = null;

/**
* @var string The dummy name
*/
public string $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

class ConcreteWithDiscriminator extends AbstractWithDiscriminator
{
/**
* @var string a concrete thing
*/
public string $instance;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\AbstractWithDiscriminator;
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild;
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild;
use Symfony\Component\Serializer\Tests\Fixtures\ConcreteWithDiscriminator;
use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux;
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummyWithContextAttribute;

Expand Down Expand Up @@ -302,6 +304,39 @@ public function testNormalizeWithNestedAttributesInConstructor()
], $test);
}

public function testDenormalizeWithDiscriminatorMapNotMappedToAProperty()
{
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

$loaderMock = new class() implements ClassMetadataFactoryInterface {
public function getMetadataFor($value): ClassMetadataInterface
{
if (ConcreteWithDiscriminator::class === $value) {
return new ClassMetadata(
AbstractWithDiscriminator::class,
new ClassDiscriminatorMapping('discr', [
'first' => ConcreteWithDiscriminator::class,
])
);
}

throw new InvalidArgumentException();
}

public function hasMetadataFor($value): bool
{
return ConcreteWithDiscriminator::class === $value;
}
};

$discriminatorResolver = new ClassDiscriminatorFromClassMetadata($loaderMock);
$normalizer = new AbstractObjectNormalizerDummy($factory, null, new PhpDocExtractor(), $discriminatorResolver);
$serializer = new Serializer([$normalizer]);
$normalizer->setSerializer($serializer);

$this->assertInstanceOf(AbstractWithDiscriminator::class, $normalizer->denormalize(['id' => 0, 'instance' => 'test', 'name' => 'test'], ConcreteWithDiscriminator::class, 'any'));
}

public function testNormalizeWithNestedAttributesInConstructorAndDiscriminatorMap()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Expand Down