[Serializer] test: discriminator without matching property#50472
[Serializer] test: discriminator without matching property#50472soyuka wants to merge 1 commit intosymfony:6.2from
Conversation
ee61a55 to
0efb68f
Compare
|
Hi, here's what I was able to figure out: unfortunately your test case doesn't really illustrate the real issue since $loaderMock = new class() implements ClassMetadataFactoryInterface {
public function getMetadataFor($value): ClassMetadataInterface
{
if (ConcreteWithDiscriminator::class === $value) {
return new ClassMetadata(ConcreteWithDiscriminator::class);
}
throw new InvalidArgumentException();
}
public function hasMetadataFor($value): bool
{
return ConcreteWithDiscriminator::class === $value;
}
};I know this because I've tested the following cases on a clean Symfony installation: $serializer->denormalize(['id' => 0, 'instance' => 'test', 'name' => 'test'], ConcreteWithDiscriminator::class, 'any');
$serializer->denormalize(['id' => 0, 'instance' => 'test', 'name' => 'test', 'discr' => 'concrete'], AbstractWithDiscriminator::class, 'any');
$serializer->denormalize(['id' => 0, 'instance' => 'test', 'name' => 'test'], AbstractWithDiscriminator::class, 'any');Both before and after #50193 the first two cases work, while the third one throws an exception: With the API Platform test case things are a little different because the $this->serializer->denormalize(
['id' => 0, 'instance' => 'test', 'name' => 'test'],
AbstractWithDiscriminator::class,
'any',
[AbstractNormalizer::OBJECT_TO_POPULATE => $object = new ConcreteWithDiscriminator()],
);Again, for this case, both before and after #50193 an exception is thrown. The only reason your test passed before is because the That being said, I do think this is a bug in Symfony since it would make sense for the last example to pass, at least in my opinion, and as far as I can tell the fix is really simple, so I'll try to open a PR. |
|
Thanks for the report and the test case to illustrate the issue! |
…actNormalizer::OBJECT_TO_POPULATE` (HypeMC) This PR was merged into the 6.2 branch. Discussion ---------- [Serializer] Fix discriminator map not working with `AbstractNormalizer::OBJECT_TO_POPULATE` | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #50472 | License | MIT | Doc PR | - Ensures that the mapped class is taken from `AbstractNormalizer::OBJECT_TO_POPULATE` if it's set. Commits ------- da82f53 [Serializer] Fix discriminator map not working with `AbstractNormalizer::OBJECT_TO_POPULATE`
#50193 breaks a test on API Platform w/ Symfony 6.2.11 where the discriminator has no matching property.
Fixture: https://github.com/api-platform/core/blob/main/tests/Fixtures/TestBundle/Entity/AbstractDummy.php
Feature: https://github.com/api-platform/core/blob/main/features/main/crud_abstract.feature#L108-L132
This adds a failing test case, but I'm unsure whether our test case misuses the discriminator feature (does it require a property matching the discriminator ?) or if the #50193 fix breaks.
ping @HypeMC wdyt can you help me on that?