Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request attributes missing for route condition #38034

Closed
kjkooistra-youwe opened this issue Sep 2, 2020 · 3 comments
Closed

Request attributes missing for route condition #38034

kjkooistra-youwe opened this issue Sep 2, 2020 · 3 comments

Comments

@kjkooistra-youwe
Copy link

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.

@xabbuh
Copy link
Member

xabbuh commented Sep 2, 2020

When the version is also a placeholder in the path, you need to use the requirements option instead:

@Route("/get/{id}", requirements={"version": "v1"})

We may want to improve that in the FOSRestBundle documentation. PR welcome over there.

@kjkooistra-youwe
Copy link
Author

kjkooistra-youwe commented Sep 2, 2020

Thanks @xabbuh . That seems to work. Also works with multiple versions by using "v1|v2", in case anyone encounters this. I created a pull request as requested.

@xabbuh
Copy link
Member

xabbuh commented Sep 2, 2020

Thanks for confirming. Let's close here then.

@xabbuh xabbuh closed this as completed Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants