Skip to content

[Serializer] Add CDATA_WRAPPING_NAME_PATTERN support to XmlEncoder #60355

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
merged 1 commit into from
Jul 26, 2025

Conversation

Deltachaos
Copy link
Contributor

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

Add CDATA_WRAPPING_NAME_PATTERN support to XmlEncoder

This PR introduces a new option for the Symfony Serializer component's XmlEncoder: CDATA_WRAPPING_NAME_PATTERN.

While Symfony already provides CDATA_WRAPPING_PATTERN to wrap field content in CDATA based on a regex match, this new feature enables CDATA wrapping based on the field name—regardless of the field's content.

Example use case:

$this->encoder->encode($data, 'xml', [
    XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '(firstname|lastname)'
]);

This ensures that fields named firstname or lastname are always wrapped in CDATA tags, even if their content doesn't match the typical pattern used by CDATA_WRAPPING_PATTERN.

Motivation:

  • Consistent Output: In many integration scenarios (e.g., with legacy systems, XML consumers, or strict XSD schemas), consumers expect certain fields to always use CDATA sections—even when their content doesn't require escaping. This option allows for deterministic XML output by name.

  • Better Control: It separates concerns: one can now configure CDATA-wrapping for content-based needs and field-based policies independently.

  • Backward Compatible: The feature is fully optional and does not interfere with existing behavior unless the option is explicitly set.

@Deltachaos Deltachaos requested a review from dunglas as a code owner May 6, 2025 09:16
@carsonbot carsonbot added this to the 7.3 milestone May 6, 2025
@Deltachaos Deltachaos force-pushed the feature/cdata-names branch from ad5bdb6 to 7477348 Compare May 6, 2025 09:16
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Copy link
Contributor

@Spomky Spomky left a comment

Choose a reason for hiding this comment

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

Hi @Deltachaos,

This feature looks good to me.
Just one comment on the test.

Regards.

@Deltachaos Deltachaos force-pushed the feature/cdata-names branch from c37ccd5 to 98b9edc Compare July 24, 2025 07:35
@Deltachaos Deltachaos force-pushed the feature/cdata-names branch from 20696f5 to c4f363c Compare July 24, 2025 07:45
@Deltachaos
Copy link
Contributor Author

@stof I have updated the PR, would be happy if you could have a look. I also have a related PR open #60228

@OskarStark OskarStark changed the title [Serializer] Add CDATA_WRAPPING_NAME_PATTERN support to XmlEncoder [Serializer] Add CDATA_WRAPPING_NAME_PATTERN support to XmlEncoder Jul 24, 2025
@fabpot
Copy link
Member

fabpot commented Jul 26, 2025

Thank you @Deltachaos.

@fabpot fabpot merged commit 6832421 into symfony:7.4 Jul 26, 2025
10 of 11 checks passed
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.

6 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy