Skip to content

[Serializer] XML ignore="false" on attribute not respected #46463

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
likeuntomurphy opened this issue May 26, 2022 · 0 comments
Closed

[Serializer] XML ignore="false" on attribute not respected #46463

likeuntomurphy opened this issue May 26, 2022 · 0 comments

Comments

@likeuntomurphy
Copy link

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.

@fabpot fabpot closed this as completed Jun 5, 2022
fabpot added a commit that referenced this issue Jun 5, 2022
This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Fix ignore attribute in Xml files

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #46463
| License       | MIT

Before this PR `foo` was ignored. Now it is not.

```xml
<class name="Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummy">
    <attribute name="foo" ignore="false" />
</class>
```
Instead of cast we now use `XmlUtils::phpize` like others XmlLoaders.

Commits
-------

6cdcb1b [Serializer] Fix ignore attribute in Xml files
@fabpot fabpot closed this as completed Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants