8000 [HttpKernel] Fix issue when no query parameter with mapQueryString by AurelienPillevesse · Pull Request #51036 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] Fix issue when no query parameter with mapQueryString #51036

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

Closed
wants to merge 1 commit into from
Closed

[HttpKernel] Fix issue when no query parameter with mapQueryString #51036

wants to merge 1 commit into from

Conversation

AurelienPillevesse
Copy link
Contributor
Q A
Branch? 6.3
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR
#[Route('/hello', methods: ['GET'])]
public function getHello(
    #[MapQueryString] HelloGetRequest $helloGetRequest
): Response {
    var_dump($helloGetRequest);
    die;
}

final class HelloGetRequest
{
    #[Assert\NotBlank(allowNull: true)]
    #[Assert\Date]
    public ?string $dateDebut = null;

    #[Assert\NotBlank(allowNull: true)]
    #[Assert\Date]
    public ?string $dateFin = null;
}

If I call the URL http://localhost:8080/hello, I will receive a 404 Not Found error because there is nothing in the query parameters.
The idea is to have the object HelloGetRequest initialized to access it and if I try to get $helloGetRequest->dateDebut, null will be displayed.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Jul 20, 2023

You should write the argument this way instead:

 #[MapQueryString] HelloGetRequest $helloGetRequest = new HelloGetRequest()

@AurelienPillevesse
Copy link
Contributor Author

Thanks for the reply @nicolas-grekas, it might be interesting to add this information somewhere in the documentation to share the knowledge.

@nicolas-grekas
Copy link
Member

You're right. Can you please consider a PR to the doc?

@kubk
Copy link
kubk commented Aug 3, 2023

You should write the argument this way instead:

 #[MapQueryString] HelloGetRequest $helloGetRequest = new HelloGetRequest()

@nicolas-grekas While I understand that it can be solved on the user's end, I can't imagine why anyone would want the route with query params to give a 404 error when there are no query parameters. Query parameters are always optional. If the issue isn't going to be fixed, then developers should provide a default value for all the routes using #[MapQueryString]. Which is easy to forget (but can be solved by new static analysis rules)

@nicolas-grekas
Copy link
Member

See also #50690 (comment)

But there are too many comments on this topic scattered on closed issues.
Please open a dedicated one if you want any follow up on the topic.

On my side, I see no issues with #[MapQueryString] HelloGetRequest $helloGetRequest = null, and I'm sure there are URLs that mandate a query string so the use case should be covered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0