Skip to content

Commit c70be09

Browse files
bug symfony#46199 [HttpKernel] Handle previously converted DateTime arguments (mbabker)
This PR was merged into the 6.1 branch. Discussion ---------- [HttpKernel] Handle previously converted `DateTime` arguments | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix sensiolabs/SensioFrameworkExtraBundle#770 | License | MIT | Doc PR | N/A I'm not sure I like this fix, but given the order of operations between the param converters in SensioFrameworkExtraBundle and the controller argument resolvers in the HttpKernel component, this is probably going to cause the least number of headaches for users when upgrading. And in some ways, this is the same problem as symfony#40333 but in reverse. Because the param converters are triggered on the `kernel.controller` event, they will handle converting values before the controller argument resolvers are fired. This means that a typehinted `DateTimeInterface` will potentially be handled twice (once by the param converter, once by the argument resolver). To avoid the `TypeError` noted in the issue, a practical fix is to gracefully handle a previously converted value in this resolver. As the other options aren't great (removing services from the container or turning off the param converters in full, which has other side effects), this is probably the most practical fix. Commits ------- a48ecb6 Handle previously converted DateTime arguments
2 parents 422377a + a48ecb6 commit c70be09

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/DateTimeValueResolver.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
4040
{
4141
$value = $request->attributes->get($argument->getName());
4242

43+
if ($value instanceof \DateTimeInterface) {
44+
yield $value;
45+
46+
return;
47+
}
48+
4349
if ($argument->isNullable() && !$value) {
4450
yield null;
4551

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/DateTimeValueResolverTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ public function testNullableWithEmptyAttribute()
113113
$this->assertNull($results[0]);
114114
}
115115

116+
public function testPreviouslyConvertedAttribute()
117+
{
118+
$resolver = new DateTimeValueResolver();
119+
120+
$argument = new ArgumentMetadata('dummy', \DateTime::class, false, false, null, true);
121+
$request = self::requestWithAttributes(['dummy' => $datetime = new \DateTime()]);
122+
123+
/** @var \Generator $results */
124+
$results = $resolver->resolve($request, $argument);
125+
$results = iterator_to_array($results);
126+
127+
$this->assertCount(1, $results);
128+
$this->assertSame($datetime, $results[0]);
129+
}
130+
116131
public function testCustomClass()
117132
{
118133
date_default_timezone_set('UTC');

0 commit comments

Comments
 (0)