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

Skip to content

Commit fe14dc1

Browse files
committed
Improve exception message when EntityValueResolver gets no mapping information
1 parent 1a8b82e commit fe14dc1

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\HttpFoundation\Request;
2222
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
2323
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
24+
use Symfony\Component\HttpKernel\Exception\NearMissValueResolverException;
2425
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2526

2627
/**
@@ -68,7 +69,11 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
6869
} elseif (false === $object = $this->find($manager, $request, $options, $argument)) {
6970
// find by criteria
7071
if (!$criteria = $this->getCriteria($request, $options, $manager, $argument)) {
71-
return [];
72+
if (!class_exists(NearMissValueResolverException::class)) {
73+
return [];
74+
}
75+
76+
throw new NearMissValueResolverException(sprintf('Cannot find mapping for "%s": declare one using either the #[MapEntity] attribute or mapped route parameters.', $options->class));
7277
}
7378
try {
7479
$object = $manager->getRepository($options->class)->findOneBy($criteria);
@@ -185,7 +190,7 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
185190

186191
return $criteria;
187192
} elseif (null === $mapping) {
188-
trigger_deprecation('symfony/doctrine-bridge', '7.1', 'Relying on auto-mapping for Doctrine entities is deprecated for argument $%s of "%s": declare the identifier using either the #[MapEntity] attribute or mapped route parameters.', $argument->getName(), method_exists($argument, 'getControllerName') ? $argument->getControllerName() : 'n/a');
193+
trigger_deprecation('symfony/doctrine-bridge', '7.1', 'Relying on auto-mapping for Doctrine entities is deprecated for argument $%s of "%s": declare the mapping using either the #[MapEntity] attribute or mapped route parameters.', $argument->getName(), method_exists($argument, 'getControllerName') ? $argument->getControllerName() : 'n/a');
189194
$mapping = $request->attributes->keys();
190195
}
191196

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Reset the manager registry using native lazy objects when applicable
88
* Deprecate the `DoctrineExtractor::getTypes()` method, use `DoctrineExtractor::getType()` instead
99
* Add support for `Symfony\Component\Clock\DatePoint` as `DatePointType` Doctrine type
10+
* Improve exception message when `EntityValueResolver` gets no mapping information
1011

1112
7.2
1213
---

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\ExpressionLanguage\SyntaxError;
2525
use Symfony\Component\HttpFoundation\Request;
2626
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
27+
use Symfony\Component\HttpKernel\Exception\NearMissValueResolverException;
2728
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2829

2930
class EntityValueResolverTest extends TestCase
@@ -75,6 +76,11 @@ public function testResolveWithNoIdAndDataOptional()
7576
$request = new Request();
7677
$argument = $this->createArgument(null, new MapEntity(), 'arg', true);
7778

79+
if (class_exists(NearMissValueResolverException::class)) {
80+
$this->expectException(NearMissValueResolverException::class);
81+
$this->expectExceptionMessage('Cannot find mapping for "stdClass": declare one using either the #[MapEntity] attribute or mapped route parameters.');
82+
}
83+
7884
$this->assertSame([], $resolver->resolve($request, $argument));
7985
}
8086

@@ -94,6 +100,11 @@ public function testResolveWithStripNulls()
94100
$manager->expects($this->never())
95101
->method('getRepository');
96102

103+
if (class_exists(NearMissValueResolverException::class)) {
104+
$this->expectException(NearMissValueResolverException::class);
105+
$this->expectExceptionMessage('Cannot find mapping for "stdClass": declare one using either the #[MapEntity] attribute or mapped route parameters.');
106+
}
107+
97108
$this->assertSame([], $resolver->resolve($request, $argument));
98109
}
99110

@@ -262,6 +273,11 @@ public function testResolveGuessOptional()
262273

263274
$manager->expects($this->never())->method('getRepository');
264275

276+
if (class_exists(NearMissValueResolverException::class)) {
277+
$this->expectException(NearMissValueResolverException::class);
278+
$this->expectExceptionMessage('Cannot find mapping for "stdClass": declare one using either the #[MapEntity] attribute or mapped route parameters.');
279+
}
280+
265281
$this->assertSame([], $resolver->resolve($request, $argument));
266282
}
267283

0 commit comments

Comments
 (0)