Skip to content

Commit fe865c2

Browse files
MatTheCatnicolas-grekas
authored andcommitted
[DoctrineBridge] Do not ignore null IDs in EntityValueResolver
1 parent 73034b4 commit fe865c2

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

ArgumentResolver/EntityValueResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
7474
if (null === $object = $this->findViaExpression($class, $request, $options->expr, $options)) {
7575
$errorMessage = sprintf('The expression "%s" returned null', $options->expr);
7676
}
77-
// find by identifier?
77+
// find by identifier?
7878
} elseif (false === $object = $this->find($class, $request, $options, $name)) {
7979
// find by criteria
8080
if (false === $object = $this->findOneBy($class, $request, $options)) {
@@ -123,7 +123,7 @@ private function find(string $class, Request $request, MapEntity $options, strin
123123

124124
$id = $this->getIdentifier($request, $options, $name);
125125
if (false === $id || null === $id) {
126-
return false;
126+
return $id;
127127
}
128128

129129
$objectManager = $this->getManager($options->objectManager, $class);

Tests/ArgumentResolver/EntityValueResolverTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Bridge\Doctrine\Tests\ArgumentResolver;
413

514
use Doctrine\DBAL\Types\ConversionException;
@@ -214,6 +223,21 @@ public function testApplyWithId(string|int $id)
214223
$this->assertYieldEquals([$object], $ret);
215224
}
216225

226+
public function testApplyWithNullId()
227+
{
228+
$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
229+
$converter = new EntityValueResolver($registry);
230+
231+
$request = new Request();
232+
$request->attributes->set('id', null);
233+
234+
$argument = $this->createArgument(isNullable: true);
235+
236+
$ret = $converter->resolve($request, $argument);
237+
238+
$this->assertYieldEquals([null], $ret);
239+
}
240+
217241
public function testApplyWithConversionFailedException()
218242
{
219243
$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
@@ -276,7 +300,7 @@ public function testApplyGuessOptional()
276300
$converter = new EntityValueResolver($registry);
277301

278302
$request = new Request();
279-
$request->attributes->set('arg', null);
303+
$request->attributes->set('guess', null);
280304

281305
$argument = $this->createArgument('stdClass', new MapEntity(), 'arg', true);
282306

0 commit comments

Comments
 (0)