diff --git a/src/Construction/DoctrineObjectConstructor.php b/src/Construction/DoctrineObjectConstructor.php index 9a64e6305..e27064903 100644 --- a/src/Construction/DoctrineObjectConstructor.php +++ b/src/Construction/DoctrineObjectConstructor.php @@ -107,11 +107,19 @@ public function construct(DeserializationVisitorInterface $visitor, ClassMetadat return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context); } + $identifierValue = null; if (is_object($data) && 'SimpleXMLElement' === get_class($data)) { - $identifierList[$name] = (string) $data->{$propertyMetadata->serializedName}; + $identifierValue = (string) $data->{$propertyMetadata->serializedName}; } else { - $identifierList[$name] = $data[$propertyMetadata->serializedName]; + $identifierValue = $data[$propertyMetadata->serializedName]; } + + if (null === $identifierValue) { + // Doctrine doesn't support null values in identifier values. + return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context); + } + + $identifierList[$name] = $identifierValue; } if (empty($identifierList)) { diff --git a/tests/Serializer/Doctrine/ObjectConstructorTest.php b/tests/Serializer/Doctrine/ObjectConstructorTest.php index 9ace32a94..31b99ad1e 100644 --- a/tests/Serializer/Doctrine/ObjectConstructorTest.php +++ b/tests/Serializer/Doctrine/ObjectConstructorTest.php @@ -86,6 +86,21 @@ public function testFindEntity() self::assertEquals($author, $authorFetched); } + public function testFindEntityNullIdentifierUsesFallback() + { + $author = new Author('John'); + $fallback = $this->getMockBuilder(ObjectConstructorInterface::class)->getMock(); + $fallback->expects($this->once())->method('construct')->willReturn($author); + + $type = ['name' => Author::class, 'params' => []]; + $class = $this->driver->loadMetadataForClass(new \ReflectionClass(Author::class)); + + $constructor = new DoctrineObjectConstructor($this->registry, $fallback); + $authorFetched = $constructor->construct($this->visitor, $class, ['id' => null], $type, $this->context); + + self::assertEquals($author, $authorFetched); + } + public function testFindEntityExcludedByGroupsUsesFallback() { $graph = $this->createMock(GraphNavigatorInterface::class);