Skip to content

[Serializer] [ObjectNormalizer] Use bool filter when FILTER_BOOL is set #57541

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

Merged

Conversation

mazumba-nx
Copy link

Q A
Branch? 7.1
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #57540
License MIT

With 7.1 it is possible to map query booleans to php bool parameters (https://symfony.com/blog/new-in-symfony-7-1-misc-improvements-part-3#mapping-boolean-query-string-parameters). But as we found out, this only works when the DTO is initialized via construct. Otherwise the FILTER_BOOL flag will be ignored and e.g. "false" will be deserialized as true.

To fix this, I suggest to look for the FILTER_BOOL in the context and apply the filter, when the type is bool and the data is string.

@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 7.2 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

Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nicolas-grekas nicolas-grekas force-pushed the bugfix-handle-bool-properties-in-resolver branch from d2a2cbd to 6e657e8 Compare June 28, 2024 07:42
@nicolas-grekas
Copy link
Member

Thank you @mazumba-nx.

@nicolas-grekas nicolas-grekas merged commit 0af0679 into symfony:7.1 Jun 28, 2024
8 of 9 checks passed
@fabpot fabpot mentioned this pull request Jun 28, 2024
nicolas-grekas added a commit that referenced this pull request Jan 17, 2025
…ER_BOOL (DjordyKoert)

This PR was submitted for the 7.2 branch but it was squashed and merged into the 7.1 branch instead.

Discussion
----------

[Serializer] [ObjectNormalizer] Filter int when using FILTER_BOOL

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix ...
| License       | MIT

Improves on #57541 to also allow numeric values `1` and `0` with `FILTER_BOOL => true`

Currently attempting to use the Serializer component to deserialize to deserialize a `1` or `0` result in the following error:
```
The type of the "booleanProperty" attribute for class "App\MyDTO" must be one of "bool" ("int" given).
```

Commits
-------

5288eba [Serializer] [ObjectNormalizer] Filter int when using FILTER_BOOL
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.

5 participants