-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected: Tested with 5.1.5
Description
I ran into this issue when setting up the FOSRestBundle.
https://symfony.com/doc/3.x/bundles/FOSRestBundle/versioning.html#how-to-match-a-specific-version-in-my-routing
When I tried the condition mentioned there and sent a request to http://localdomain.tld/api/v1/element/get/123, it resulted in a route not found exception. Changing the match value from 'v1' to null resulted in a successful response. It seems the request attributes are not available for the condition.
How to reproduce
Since I figured it might be related to an installed bundle, I set up a separate skeleton with the minimal required composer packages and reproduced the issue.
mkdir project_folder
cd project_folder
composer create-project symfony/skeleton .
composer require annotations
composer require symfony/expression-language
Create src/Controller/TestController.php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api/{version}/element")
*/
class TestController extends AbstractController
{
/**
* @param Request $request
* @param int $id
* @Route("/get/{id}", condition="request.attributes.get('version') == 'v1'")
*/
public function getById(Request $request, int $id): JsonResponse
{
$data = [
[
'version' => $request->attributes->get('version'),
]
];
return new JsonResponse($data);
}
}
Request the action by navigating to http://localdomain.tld/api/v1/element/get/123
This results in: No route found for "GET /api/v1/element/get/123"
Changing 'v1' to null in the condition and reloading the request results in:
[{"version":"v1"}]
So apparently the version attribute is there, but not for the condition.