Skip to content

Commit

Permalink
[6.x] Fix RouteUrlGenerator accepting empty string for required param…
Browse files Browse the repository at this point in the history
…enter (#30714)

* Fix RouteUrlGenerator accepting empty string for required paramenter

* Fixed optional parameter passed without key, added tests

* StyleCI fixes
  • Loading branch information
MrCrayon authored and taylorotwell committed Dec 2, 2019
1 parent 767bc1b commit dfb4c1c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Routing/RouteUrlGenerator.php
Expand Up @@ -217,11 +217,13 @@ protected function replaceRouteParameters($path, array &$parameters)
*/
protected function replaceNamedParameters($path, &$parameters)
{
return preg_replace_callback('/\{(.*?)\??\}/', function ($m) use (&$parameters) {
if (isset($parameters[$m[1]])) {
return preg_replace_callback('/\{(.*?)(\?)?\}/', function ($m) use (&$parameters) {
if (isset($parameters[$m[1]]) && $parameters[$m[1]] != '') {
return Arr::pull($parameters, $m[1]);
} elseif (isset($this->defaultParameters[$m[1]])) {
return $this->defaultParameters[$m[1]];
} elseif (isset($parameters[$m[1]])) {
Arr::pull($parameters, $m[1]);
}

return $m[0];
Expand Down
17 changes: 15 additions & 2 deletions tests/Routing/RoutingUrlGeneratorTest.php
Expand Up @@ -260,6 +260,7 @@ public function testBasicRouteGeneration()
$this->assertSame('http://www.foo.com/foo/bar/taylor/breeze/otwell?wall&woz', $url->route('bar', ['taylor', 'otwell', 'wall', 'woz']));
$this->assertSame('http://www.foo.com/foo/bar', $url->route('optional'));
$this->assertSame('http://www.foo.com/foo/bar', $url->route('optional', ['baz' => null]));
$this->assertSame('http://www.foo.com/foo/bar', $url->route('optional', ['baz' => '']));
$this->assertSame('http://www.foo.com/foo/bar/taylor', $url->route('optional', 'taylor'));
$this->assertSame('http://www.foo.com/foo/bar/taylor', $url->route('optional', ['taylor']));
$this->assertSame('http://www.foo.com/foo/bar/taylor?breeze', $url->route('optional', ['taylor', 'breeze']));
Expand Down Expand Up @@ -501,7 +502,19 @@ public function testRoutesWithDomainsThroughProxy()
$this->assertSame('http://sub.foo.com/foo/bar', $url->route('foo'));
}

public function testUrlGenerationForControllersRequiresPassingOfRequiredParameters()
public function providerRouteParameters()
{
return [
[['test' => 123]],
[['one' => null, 'test' => 123]],
[['one' => '', 'test' => 123]],
];
}

/**
* @dataProvider providerRouteParameters
*/
public function testUrlGenerationForControllersRequiresPassingOfRequiredParameters($parameters)
{
$this->expectException(UrlGenerationException::class);

Expand All @@ -515,7 +528,7 @@ public function testUrlGenerationForControllersRequiresPassingOfRequiredParamete
}]);
$routes->add($route);

$this->assertSame('http://www.foo.com:8080/foo?test=123', $url->route('foo', ['test' => 123]));
$this->assertSame('http://www.foo.com:8080/foo?test=123', $url->route('foo', $parameters));
}

public function testForceRootUrl()
Expand Down

0 comments on commit dfb4c1c

Please sign in to comment.