merged branch Tobion/null-for-optional-param (PR #7369)
This PR was merged into the 2.2 branch.
Commits
-------
d7a7434
[Routing] fix url generation for optional parameter having a null value
Discussion
----------
[Routing] fix url generation for optional parameter having a null value
| Q | A
| ------------- | ---
| Bug fix? | [yes]
| New feature? | [no]
| BC breaks? | [no]
| Deprecations? | [no]
| Tests pass? | [yes|]
| Fixed tickets | [#7316]
| License | MIT
This commit is contained in:
commit
4b9d810889
@ -159,7 +159,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
||||
$optional = true;
|
||||
foreach ($tokens as $token) {
|
||||
if ('variable' === $token[0]) {
|
||||
if (!$optional || !array_key_exists($token[3], $defaults) || (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) {
|
||||
if (!$optional || !array_key_exists($token[3], $defaults) || null !== $mergedParams[$token[3]] && (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) {
|
||||
// check requirement
|
||||
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
|
||||
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]);
|
||||
|
@ -286,6 +286,13 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
|
||||
}
|
||||
|
||||
public function testNullForOptionalParameterIsIgnored()
|
||||
{
|
||||
$routes = $this->getRoutes('test', new Route('/test/{default}', array('default' => 0)));
|
||||
|
||||
$this->assertEquals('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => null)));
|
||||
}
|
||||
|
||||
public function testQueryParamSameAsDefault()
|
||||
{
|
||||
$routes = $this->getRoutes('test', new Route('/test', array('default' => 'value')));
|
||||
|
Reference in New Issue
Block a user