Skip to content

Commit

Permalink
bug #57625 [DoctrineBridge] Make EntityValueResolver return null
Browse files Browse the repository at this point in the history
…if a composite ID value is `null` (MatTheCat)

This PR was merged into the 6.4 branch.

Discussion
----------

[DoctrineBridge] Make `EntityValueResolver` return `null` if a composite ID value is `null`

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | N/A
| License       | MIT

My use-case is the upsert of an entity identified by two values. I have an `update` route with these two values as route parameters, and a `create` route with only one. In that case the second value is `null` but the `EntityValueResolver` will still call the repository’s `find` method, resulting in a `MissingIdentifierField` exception:

> The identifier [VALUE] is missing for a query of [ENTITY]

This PR makes the `EntityValueResolver` return `null` in this case, like when a scalar ID is `null`.

Commits
-------

87f18842a8 [DoctrineBridge] Make `EntityValueResolver` return `null` if a composite ID value is `null`
  • Loading branch information
xabbuh committed Jul 28, 2024
2 parents 0de9662 + e6e456a commit 24ef8e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ArgumentResolver/EntityValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ private function find(ObjectManager $manager, Request $request, MapEntity $optio
if (false === $id || null === $id) {
return $id;
}
if (\is_array($id) && \in_array(null, $id, true)) {
return null;
}

if ($options->evictCache && $manager instanceof EntityManagerInterface) {
$cacheProvider = $manager->getCache();
Expand Down
14 changes: 14 additions & 0 deletions Tests/ArgumentResolver/EntityValueResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ public function testResolveWithNullId()
$this->assertSame([null], $resolver->resolve($request, $argument));
}

public function testResolveWithArrayIdNullValue()
{
$manager = $this->createMock(ObjectManager::class);
$registry = $this->createRegistry($manager);
$resolver = new EntityValueResolver($registry);

$request = new Request();
$request->attributes->set('nullValue', null);

$argument = $this->createArgument(entity: new MapEntity(id: ['nullValue']), isNullable: true,);

$this->assertSame([null], $resolver->resolve($request, $argument));
}

public function testResolveWithConversionFailedException()
{
$manager = $this->getMockBuilder(ObjectManager::class)->getMock();
Expand Down

0 comments on commit 24ef8e1

Please sign in to comment.