Skip to content

Commit

Permalink
feature #30390 [FrameworkBundle] Fix UrlGenerator::generate to return…
Browse files Browse the repository at this point in the history
… an empty string instead of null (Emmanuel BORGES)

This PR was squashed before being merged into the 4.3-dev branch (closes #30390).

Discussion
----------

[FrameworkBundle] Fix UrlGenerator::generate to return an empty string instead of null

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30306
| License       | MIT

Fix #30306 : Controller::generateUrl() must be of the type string, null returned

Commits
-------

c5b1247 [FrameworkBundle] Fix UrlGenerator::generate to return an empty string instead of null
  • Loading branch information
fabpot committed Mar 4, 2019
2 parents 1ad6f6f + c5b1247 commit a75dd9f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions UPGRADE-4.3.md
Expand Up @@ -37,6 +37,7 @@ FrameworkBundle
* Not passing the project directory to the constructor of the `AssetsInstallCommand` is deprecated. This argument will
be mandatory in 5.0.
* Deprecated the "Psr\SimpleCache\CacheInterface" / "cache.app.simple" service, use "Symfony\Contracts\Cache\CacheInterface" / "cache.app" instead.
* The `generate()` method of the `UrlGenerator` class can return an empty string instead of null.

HttpFoundation
--------------
Expand Down
Expand Up @@ -20,7 +20,7 @@
* The possible configurations and use-cases:
* - setStrictRequirements(true): Throw an exception for mismatching requirements. This
* is mostly useful in development environment.
* - setStrictRequirements(false): Don't throw an exception but return null as URL for
* - setStrictRequirements(false): Don't throw an exception but return an empty string as URL for
* mismatching requirements and log the problem. Useful when you cannot control all
* params because they come from third party libs but don't want to have a 404 in
* production environment. It should log the mismatch so one can review it.
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Routing/Generator/UrlGenerator.php
Expand Up @@ -171,7 +171,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
$this->logger->error($message, ['parameter' => $varName, 'route' => $name, 'expected' => $token[2], 'given' => $mergedParams[$varName]]);
}

return;
return '';
}

$url = $token[1].$mergedParams[$varName].$url;
Expand Down Expand Up @@ -226,7 +226,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
$this->logger->error($message, ['parameter' => $token[3], 'route' => $name, 'expected' => $token[2], 'given' => $mergedParams[$token[3]]]);
}

return;
return '';
}

$routeHost = $token[1].$mergedParams[$token[3]].$routeHost;
Expand Down
Expand Up @@ -203,7 +203,7 @@ public function testGenerateForRouteWithInvalidOptionalParameterNonStrict()
$routes = $this->getRoutes('test', new Route('/testing/{foo}', ['foo' => '1'], ['foo' => 'd+']));
$generator = $this->getGenerator($routes);
$generator->setStrictRequirements(false);
$this->assertNull($generator->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL));
$this->assertSame('', $generator->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL));
}

public function testGenerateForRouteWithInvalidOptionalParameterNonStrictWithLogger()
Expand All @@ -214,7 +214,7 @@ public function testGenerateForRouteWithInvalidOptionalParameterNonStrictWithLog
->method('error');
$generator = $this->getGenerator($routes, [], $logger);
$generator->setStrictRequirements(false);
$this->assertNull($generator->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL));
$this->assertSame('', $generator->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL));
}

public function testGenerateForRouteWithInvalidParameterButDisabledRequirementsCheck()
Expand Down Expand Up @@ -489,7 +489,7 @@ public function testUrlWithInvalidParameterInHostInNonStrictMode()
$routes = $this->getRoutes('test', new Route('/', [], ['foo' => 'bar'], [], '{foo}.example.com'));
$generator = $this->getGenerator($routes);
$generator->setStrictRequirements(false);
$this->assertNull($generator->generate('test', ['foo' => 'baz'], UrlGeneratorInterface::ABSOLUTE_PATH));
$this->assertSame('', $generator->generate('test', ['foo' => 'baz'], UrlGeneratorInterface::ABSOLUTE_PATH));
}

public function testHostIsCaseInsensitive()
Expand Down

0 comments on commit a75dd9f

Please sign in to comment.