Skip to content

Commit

Permalink
#1297: Fix deserialization of doctrine entity with null identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Mar 25, 2021
1 parent ea54838 commit b8303f9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Construction/DoctrineObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,15 @@ 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) {
$identifierList[$name] = $identifierValue;
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Serializer/Doctrine/ObjectConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b8303f9

Please sign in to comment.