simple-php-router
simple-php-router copied to clipboard
LoadableRoute::setUrl() doesn't empty the parameter array
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.