Skip to content

[HttpKernel] #[MapQueryParameter] Problem with nullable parameters #50339

@szepczynski

Description

@szepczynski

Symfony version(s) affected

6.3.0-BETA3

Description

Hi,

In documentation is an example with optional nullable string parameter (?string $theme = null).

Symfony has allowed to map route parameters to controller arguments since its first version. In Symfony 6.3 you will also be able to map query string parameters to specific controller arguments. To do so, use the new #[MapQueryParameter] attribute:

public function indexAction(
    #[MapQueryParameter] array $ids,
    #[MapQueryParameter] string $firstName,
    #[MapQueryParameter] bool $required,
    #[MapQueryParameter] int $age,
    #[MapQueryParameter] string $category = '',
    #[MapQueryParameter] ?string $theme = null,
)

If the query string of the incoming request is /?ids[]=1&ids[]=2&firstName=Ruud&required=3&age=123, this code will make the controller arguments to have these values:

$ids = ['1', '2']
$firstName = "Ruud"
$required = false
$age = 123
$category = ''
$theme = null

I test similar example on v6.3.0-BETA3 on my application but nullable query string parameter doesn't work.

    #[Route(path: '/')]
    public function indexAction(
        #[MapQueryParameter] ?string $theme = null
    ): Response {
        return new Response('test');
    }
  1. GET /?theme works

  2. GET / leads to RuntimeException (Controller "App\Controller\IndexController::indexAction" requires that you provide a value for the "$theme" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.)

How to reproduce

Example: https://github.com/szepczynski/symfony-6.3-map-query-parameter-bug-reproduction/

Possible Solution

No response

Additional Context

No response

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