Description
Symfony version(s) affected
5.4
Description
The serializer-mapping-1.0-xsd
includes a boolean ignore
attribute on the attribute
element, but ignore="false"
is not interpreted as false
. The attribute is ignored, because the XMLFileLoader
casts the attribute to bool
, which is always true because it is an instance of SimpleXMLElement
.
How to reproduce
Clone /likeuntomurphy/symfony-serializer-xml-ignore-reproducer and run docker run --rm -it -v ${PWD}:/app -w /app php:8.1-cli php bin/console serialize-myobject
.
The expectation is that the command finishes with no output. However, an assertion fails:
In SerializeMyObject.php line 30:
assert($normalized['explicitlyNotIgnored'] ?? 'ignored' === 'not ignored')
Possible Solution
Use filter_var((string) $attribute['ignore'], FILTER_VALIDATE_BOOLEAN)
or strictly compare the attribute value against "true"
or "false"
. A quick scan of vendor
also shows that Doctrine Migrations uses a BooleanStringFormatter
on xsd:boolean
node values.
Additional Context
There are no tests for a condition where ignore="false"
is included — I’m not certain if that is an oversight or if the expectation is that no one will ever do this. This is why I haven’t made a PR.
To my mind there should be parity between the schema and the AttributeMetadata
values.