Skip to content

Request attributes missing for route condition #38034

@kjkooistra-youwe

Description

@kjkooistra-youwe

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy