Skip to content

Commit dd4e78c

Browse files
committed
minor symfony#21379 [Routing] Fix BC break in AnnotationClassLoader defaults attributes handling (chalasr)
This PR was merged into the 2.7 branch. Discussion ---------- [Routing] Fix BC break in AnnotationClassLoader defaults attributes handling | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony@77289b9#commitcomment-20572462 | License | MIT | Doc PR | n/a This fixes a BC break introduced in symfony#21333. Instead of removing the automatic request attributes creation, we keep it but only for attributes that are mandatory (i.e. present in the route path). Thanks to @iltar for the idea. Commits ------- 1d298f0 [Routing] Fix BC break in AnnotationClassLoader defaults attributes handling
2 parents 388be9d + 1d298f0 commit dd4e78c

File tree

8 files changed

+126
-3
lines changed

8 files changed

+126
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@
7979
"ircmaxell/password-compat": "~1.0",
8080
"ocramius/proxy-manager": "~0.4|~1.0|~2.0",
8181
"symfony/phpunit-bridge": "~3.2",
82-
"egulias/email-validator": "~1.2,>=1.2.1"
82+
"egulias/email-validator": "~1.2,>=1.2.1",
83+
"sensio/framework-extra-bundle": "^3.0.2"
8384
},
8485
"autoload": {
8586
"psr-4": {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
class AnnotatedControllerTest extends WebTestCase
15+
{
16+
/**
17+
* @dataProvider getRoutes
18+
*/
19+
public function testAnnotatedController($path, $expectedValue)
20+
{
21+
$client = $this->createClient(array('test_case' => 'AnnotatedController', 'root_config' => 'config.yml'));
22+
$client->request('GET', '/annotated'.$path);
23+
24+
$this->assertSame(200, $client->getResponse()->getStatusCode());
25+
$this->assertSame($expectedValue, $client->getResponse()->getContent());
26+
}
27+
28+
public function getRoutes()
29+
{
30+
return array(
31+
array('/null_request', 'Symfony\Component\HttpFoundation\Request'),
32+
array('/null_argument', ''),
33+
array('/null_argument_with_route_param', ''),
34+
array('/null_argument_with_route_param/value', 'value'),
35+
array('/argument_with_route_param_and_default', 'value'),
36+
array('/argument_with_route_param_and_default/custom', 'custom'),
37+
);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\Routing\Annotation\Route;
17+
18+
class AnnotatedController
19+
{
20+
/**
21+
* @Route("/null_request", name="null_request")
22+
*/
23+
public function requestDefaultNullAction(Request $request = null)
24+
{
25+
return new Response($request ? get_class($request) : null);
26+
}
27+
28+
/**
29+
* @Route("/null_argument", name="null_argument")
30+
*/
31+
public function argumentDefaultNullWithoutRouteParamAction($value = null)
32+
{
33+
return new Response($value);
34+
}
35+
36+
/**
37+
* @Route("/null_argument_with_route_param/{value}", name="null_argument_with_route_param")
38+
*/
39+
public function argumentDefaultNullWithRouteParamAction($value = null)
40+
{
41+
return new Response($value);
42+
}
43+
44+
/**
45+
* @Route("/argument_with_route_param_and_default/{value}", defaults={"value": "value"}, name="argument_with_route_param_and_default")
46+
*/
47+
public function argumentWithoutDefaultWithRouteParamAndDefaultAction($value)
48+
{
49+
return new Response($value);
50+
}
51+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
13+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
14+
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
15+
16+
return array(
17+
new FrameworkBundle(),
18+
new TestBundle(),
19+
new SensioFrameworkExtraBundle(),
20+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
imports:
2+
- { resource: ../config/default.yml }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
annotated_controller:
2+
prefix: /annotated
3+
resource: "@TestBundle/Controller/AnnotatedController.php"
4+
type: annotation

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"symfony/http-foundation": "~2.7",
2626
"symfony/http-kernel": "~2.7.23|~2.8.16",
2727
"symfony/filesystem": "~2.3",
28-
"symfony/routing": "~2.6,>2.6.4",
28+
"symfony/routing": "~2.7.24|~2.8.17",
2929
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",
3030
"symfony/security-csrf": "~2.6",
3131
"symfony/stopwatch": "~2.3",
@@ -45,7 +45,8 @@
4545
"symfony/expression-language": "~2.6",
4646
"symfony/process": "~2.0,>=2.0.5",
4747
"symfony/validator": "~2.5",
48-
"symfony/yaml": "~2.0,>=2.0.5"
48+
"symfony/yaml": "~2.0,>=2.0.5",
49+
"sensio/framework-extra-bundle": "^3.0.2"
4950
},
5051
"suggest": {
5152
"symfony/console": "For using the console commands",

src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
138138
}
139139

140140
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
141+
foreach ($method->getParameters() as $param) {
142+
if (false !== strpos($globals['path'].$annot->getPath(), sprintf('{%s}', $param->getName())) && !isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
143+
$defaults[$param->getName()] = $param->getDefaultValue();
144+
}
145+
}
141146
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
142147
$options = array_replace($globals['options'], $annot->getOptions());
143148
$schemes = array_merge($globals['schemes'], $annot->getSchemes());

0 commit comments

Comments
 (0)