Skip to content

Commit

Permalink
minor #21379 [Routing] Fix BC break in AnnotationClassLoader defaults…
Browse files Browse the repository at this point in the history
… 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 | 77289b9#commitcomment-20572462
| License       | MIT
| Doc PR        | n/a

This fixes a BC break introduced in #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
  • Loading branch information
fabpot committed Jan 24, 2017
2 parents 388be9d + 1d298f0 commit dd4e78c
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -79,7 +79,8 @@
"ircmaxell/password-compat": "~1.0",
"ocramius/proxy-manager": "~0.4|~1.0|~2.0",
"symfony/phpunit-bridge": "~3.2",
"egulias/email-validator": "~1.2,>=1.2.1"
"egulias/email-validator": "~1.2,>=1.2.1",
"sensio/framework-extra-bundle": "^3.0.2"
},
"autoload": {
"psr-4": {
Expand Down
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

class AnnotatedControllerTest extends WebTestCase
{
/**
* @dataProvider getRoutes
*/
public function testAnnotatedController($path, $expectedValue)
{
$client = $this->createClient(array('test_case' => 'AnnotatedController', 'root_config' => 'config.yml'));
$client->request('GET', '/annotated'.$path);

$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertSame($expectedValue, $client->getResponse()->getContent());
}

public function getRoutes()
{
return array(
array('/null_request', 'Symfony\Component\HttpFoundation\Request'),
array('/null_argument', ''),
array('/null_argument_with_route_param', ''),
array('/null_argument_with_route_param/value', 'value'),
array('/argument_with_route_param_and_default', 'value'),
array('/argument_with_route_param_and_default/custom', 'custom'),
);
}
}
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class AnnotatedController
{
/**
* @Route("/null_request", name="null_request")
*/
public function requestDefaultNullAction(Request $request = null)
{
return new Response($request ? get_class($request) : null);
}

/**
* @Route("/null_argument", name="null_argument")
*/
public function argumentDefaultNullWithoutRouteParamAction($value = null)
{
return new Response($value);
}

/**
* @Route("/null_argument_with_route_param/{value}", name="null_argument_with_route_param")
*/
public function argumentDefaultNullWithRouteParamAction($value = null)
{
return new Response($value);
}

/**
* @Route("/argument_with_route_param_and_default/{value}", defaults={"value": "value"}, name="argument_with_route_param_and_default")
*/
public function argumentWithoutDefaultWithRouteParamAndDefaultAction($value)
{
return new Response($value);
}
}
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;

return array(
new FrameworkBundle(),
new TestBundle(),
new SensioFrameworkExtraBundle(),
);
@@ -0,0 +1,2 @@
imports:
- { resource: ../config/default.yml }
@@ -0,0 +1,4 @@
annotated_controller:
prefix: /annotated
resource: "@TestBundle/Controller/AnnotatedController.php"
type: annotation
5 changes: 3 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Expand Up @@ -25,7 +25,7 @@
"symfony/http-foundation": "~2.7",
"symfony/http-kernel": "~2.7.23|~2.8.16",
"symfony/filesystem": "~2.3",
"symfony/routing": "~2.6,>2.6.4",
"symfony/routing": "~2.7.24|~2.8.17",
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",
"symfony/security-csrf": "~2.6",
"symfony/stopwatch": "~2.3",
Expand All @@ -45,7 +45,8 @@
"symfony/expression-language": "~2.6",
"symfony/process": "~2.0,>=2.0.5",
"symfony/validator": "~2.5",
"symfony/yaml": "~2.0,>=2.0.5"
"symfony/yaml": "~2.0,>=2.0.5",
"sensio/framework-extra-bundle": "^3.0.2"
},
"suggest": {
"symfony/console": "For using the console commands",
Expand Down
Expand Up @@ -138,6 +138,11 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
}

$defaults = array_replace($globals['defaults'], $annot->getDefaults());
foreach ($method->getParameters() as $param) {
if (false !== strpos($globals['path'].$annot->getPath(), sprintf('{%s}', $param->getName())) && !isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
$defaults[$param->getName()] = $param->getDefaultValue();
}
}
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
$options = array_replace($globals['options'], $annot->getOptions());
$schemes = array_merge($globals['schemes'], $annot->getSchemes());
Expand Down

0 comments on commit dd4e78c

Please sign in to comment.