simple-php-router icon indicating copy to clipboard operation
simple-php-router copied to clipboard

LoadableRoute::setUrl() doesn't empty the parameter array

Open SunflowerFuchs opened this issue 1 year ago • 0 comments

Current behavior

Lets take the following example snippet:

$route = SimpleRouter::get('/route/with/{parameter}', function() {});
$parameters  = $route->getParameters();
// $parameters == [ 'parameter' => null ]

$route->setUrl('/route/with/{differentparameter}');
$parameters = $route->getParameters();
// $parameters == [ 'differentparameter' => null ]

$route->setUrl('/route/without/parameter');
$parameters = $route->getParameters();
// $parameters == [ 'differentparameter' => null ]

The last setUrl() call doesn't empty the parameters, and instead keeps them as before.

Expected behavior

The last $parameters should be empty, since the URL has no parameters.

Context

We're currently generating an OpenAPI specification based on all of our registered routes. Since OpenAPI doesn't allow route parameters to be optional, we have to split the routes in two (/route/{optional?} to /route and /route/optional), which is easily possible with a simple clone $route followed by setUrl(). But due to the parameters not being cleared we get problems further down the line.

SunflowerFuchs avatar May 05 '23 12:05 SunflowerFuchs