Skip to content
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

#1297: Fix deserialization of doctrine entity with null identifier #1299

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/Construction/DoctrineObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
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