Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6780b02

Browse files
[DoctrineBridge] Skip throwing when entity is not found and no attribute explicitly asks for it
1 parent 79e1d8d commit 6780b02

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/Symfony/Bridge/Doctrine/ArgumentResolver/EntityValueResolver.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public function __construct(
4040

4141
public function resolve(Request $request, ArgumentMetadata $argument): array
4242
{
43-
$options = $this->getMapEntityAttribute($argument);
43+
$attribute = $argument->getAttributes(MapEntity::class, ArgumentMetadata::IS_INSTANCEOF);
44+
$options = ($attribute[0] ?? $this->defaults)->withDefaults($this->defaults, $argument->getType());
45+
4446
if (!$options->class || $options->disabled) {
4547
return [];
4648
}
@@ -66,6 +68,10 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
6668
}
6769
}
6870

71+
if (null === $object && !$attribute) {
72+
return [];
73+
}
74+
6975
if (null === $object && !$argument->isNullable()) {
7076
throw new NotFoundHttpException(sprintf('"%s" object not found by "%s".', $options->class, self::class).$message);
7177
}
@@ -201,12 +207,4 @@ private function findViaExpression(ObjectManager $manager, Request $request, Map
201207
return null;
202208
}
203209
}
204-
205-
private function getMapEntityAttribute(ArgumentMetadata $argument): MapEntity
206-
{
207-
/** @var MapEntity $options */
208-
$options = $argument->getAttributes(MapEntity::class, ArgumentMetadata::IS_INSTANCEOF)[0] ?? $this->defaults;
209-
210-
return $options->withDefaults($this->defaults, $argument->getType());
211-
}
212210
}

src/Symfony/Bridge/Doctrine/Tests/ArgumentResolver/EntityValueResolverTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,16 @@ public function testResolveWithNullId()
139139
$request = new Request();
140140
$request->attributes->set('id', null);
141141

142-
$argument = $this->createArgument(isNullable: true);
142+
$argument = $this->createArgument(isNullable: true, entity: new MapEntity());
143143

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

147-
public function testResolveWithConversionFailedException()
147+
/**
148+
* @testWith [true]
149+
* [false]
150+
*/
151+
public function testResolveWithConversionException(bool $withAttribute)
148152
{
149153
$manager = $this->getMockBuilder(ObjectManager::class)->getMock();
150154
$registry = $this->createRegistry($manager);
@@ -153,7 +157,7 @@ public function testResolveWithConversionFailedException()
153157
$request = new Request();
154158
$request->attributes->set('id', 'test');
155159

156-
$argument = $this->createArgument('stdClass', new MapEntity(id: 'id'));
160+
$argument = $this->createArgument('stdClass', $withAttribute ? new MapEntity(id: 'id') : null);
157161

158162
$repository = $this->getMockBuilder(ObjectRepository::class)->getMock();
159163
$repository->expects($this->once())
@@ -166,9 +170,13 @@ public function testResolveWithConversionFailedException()
166170
->with('stdClass')
167171
->willReturn($repository);
168172

169-
$this->expectException(NotFoundHttpException::class);
173+
if ($withAttribute) {
174+
$this->expectException(NotFoundHttpException::class);
170175

171-
$resolver->resolve($request, $argument);
176+
$resolver->resolve($request, $argument);
177+
} else {
178+
$this->assertSame([], $resolver->resolve($request, $argument));
179+
}
172180
}
173181

174182
public function testUsedProperIdentifier()

0 commit comments

Comments
 (0)