FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 704 forks source link

Route overwrites other Route #2290

Closed codeneuss closed 3 years ago

codeneuss commented 3 years ago

Hi,

I'm using Version 3.0.4 of FOSRestBundle with Symfony 4.4.18. I have a crontroller with some actions:

/**
 * @Rest\Route("/sample")
 */
class SampleController extends AbstractFOSRestController {

    /**
     * @Rest\Get("/{id}")
     */
    public function getAction($id)
    {
        //...//    
    }

    /**
     * @Rest\Get("/filter")
     */
    public function cgetFilterAction(){
        //...//
    }
}

When I call console debug:routes I get app_sample_cget_filter /rest/v1/sample/filter. But when I call console router:match /rest/v1/sample/filter I get SampleController::getAction as answer.

The same happens when I call the route in postman or browser. I recently updated from sf 3.4 to 4.4 and of course from the corresponding FosRestBundle Version to the current stable.

I've found a workaround with a requirement in the Route-Definition of the /{id} route with requirements.

Is this a bug or is it wrong configuration?

Thanks alot

codeneuss

xabbuh commented 3 years ago

This is expected. The first route that matches a request is taken into account. The router does not know that your first route should not match arbitrary strings. Possible solutions are adding requirements to more specific routes or to reorder your routes that more specific ones are defined later.

You can read more about this in the Symfony documentation: https://symfony.com/doc/current/routing.html#parameters-validation