Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FILTER_BOOL must be used with DISABLE_TYPE_ENFORCEMENT #57487

Closed
@lowwa132

Description

@lowwa132

Symfony version(s) affected

7.1.x

Description

If the context FILTER_BOOL is used alone, we may experience a NotNormalizableValueException exception like this:

The type of the "booleanProperty" attribute for class "App\MyDTO" must be one of "bool" ("string" given).

Adding DISABLE_TYPE_ENFORCEMENT context solves the issue but is counter intuitive.

How to reproduce

Here is the code I use:

$this->serializer->deserialize($binaryExcelFile, MyDTO::class . '[]', ExcelEncoder::FORMAT);
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

class MyDTO
{
    #[Serializer\Context([AbstractNormalizer::FILTER_BOOL => true, AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true])]
    public bool $booleanProperty;
}

I use a custom ExcelEncoder.

Possible Solution

With FILTER_BOOLalone, it fails in Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::validateAndDenormalize, before Symfony\Component\Serializer\Normalizer\AbstractNormalizer::applyFilterBool is applied

Maybe the applyFilterBool should be called sooner?

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