Skip to content

[Serializer] Throw NotNormalizableValueException when type is not known or not in body in discriminator map #43004

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

Merged
merged 1 commit into from
Sep 21, 2021
Merged
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
Expand Up @@ -22,7 +22,6 @@
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
Expand Down Expand Up @@ -276,12 +275,12 @@ protected function instantiateObject(array &$data, string $class, array &$contex
{
if ($this->classDiscriminatorResolver && $mapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
if (!isset($data[$mapping->getTypeProperty()])) {
throw new RuntimeException(sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty(), $class));
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty(), $class), null, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), false);
}

$type = $data[$mapping->getTypeProperty()];
if (null === ($mappedClass = $mapping->getClassForType($type))) {
throw new RuntimeException(sprintf('The type "%s" has no mapped class for the abstract object "%s".', $type, $class));
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type "%s" is not a valid value.', $type), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), true);
}

if ($mappedClass !== $class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class Php74Full
/** @var Php74Full[] */
public array $collection;
public Php74FullWithConstructor $php74FullWithConstructor;
public DummyMessageInterface $dummyMessage;
}


Expand Down
44 changes: 36 additions & 8 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
Expand Down Expand Up @@ -484,16 +483,34 @@ public function testDeserializeAndSerializeNestedInterfacedObjectsWithTheClassMe

public function testExceptionWhenTypeIsNotKnownInDiscriminator()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('The type "second" has no mapped class for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface"');
$this->serializerWithClassDiscriminator()->deserialize('{"type":"second","one":1}', DummyMessageInterface::class, 'json');
try {
$this->serializerWithClassDiscriminator()->deserialize('{"type":"second","one":1}', DummyMessageInterface::class, 'json');

$this->fail();
} catch (\Throwable $e) {
$this->assertInstanceOf(NotNormalizableValueException::class, $e);
$this->assertSame('The type "second" is not a valid value.', $e->getMessage());
$this->assertSame('string', $e->getCurrentType());
$this->assertSame(['string'], $e->getExpectedTypes());
$this->assertSame('type', $e->getPath());
$this->assertTrue($e->canUseMessageForUser());
}
}

public function testExceptionWhenTypeIsNotInTheBodyToDeserialiaze()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Type property "type" not found for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface"');
$this->serializerWithClassDiscriminator()->deserialize('{"one":1}', DummyMessageInterface::class, 'json');
try {
$this->serializerWithClassDiscriminator()->deserialize('{"one":1}', DummyMessageInterface::class, 'json');

$this->fail();
} catch (\Throwable $e) {
$this->assertInstanceOf(NotNormalizableValueException::class, $e);
$this->assertSame('Type property "type" not found for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface".', $e->getMessage());
$this->assertSame('null', $e->getCurrentType());
$this->assertSame(['string'], $e->getExpectedTypes());
$this->assertSame('type', $e->getPath());
$this->assertFalse($e->canUseMessageForUser());
}
}

public function testNotNormalizableValueExceptionMessageForAResource()
Expand Down Expand Up @@ -744,7 +761,9 @@ public function testCollectDenormalizationErrors()
"string": null
}
],
"php74FullWithConstructor": {}
"php74FullWithConstructor": {},
"dummyMessage": {
}
}';

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Expand Down Expand Up @@ -893,6 +912,15 @@ public function testCollectDenormalizationErrors()
'useMessageForUser' => true,
'message' => 'Failed to create object because the object miss the "constructorArgument" property.',
],
[
'currentType' => 'null',
'expectedTypes' => [
'string',
],
'path' => 'dummyMessage.type',
'useMessageForUser' => false,
'message' => 'Type property "type" not found for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface".',
],
];

$this->assertSame($expected, $exceptionsAsArray);
Expand Down