-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
base: 7.4
Are you sure you want to change the base?
Conversation
ad5bdb6
to
7477348
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
]]; | ||
|
||
$this->assertEquals($expected, $this->encoder->encode($source, 'xml',[ | ||
XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '(firstname|lastname|)' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the test, I did not expected to see <other><![CDATA[data]]></other>
but <other>data</other>
.
It looks like it is due to the last |
in '(firstname|lastname|)'
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes thats right. I will fix it
]]; | ||
|
||
$this->assertEquals($expected, $this->encoder->encode($source, 'xml',[ | ||
XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '(firstname|lastname|)' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using parenthesis as regex delimiter is quite uncommon. People reading the tests could think that we expect a partial pattern where we add the delimiter ourselves. I suggest changing the delimiters being used (and I also suggest using an anchored regex in that test)
@@ -440,10 +442,15 @@ private function appendNode(\DOMNode $parentNode, mixed $data, string $format, a | |||
|
|||
/** | |||
* Checks if a value contains any characters which would require CDATA wrapping. | |||
* | |||
* @param array<mixed, mixed> $context |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param array<mixed, mixed> $context | |
* @param array<string, mixed> $context |
Add
CDATA_WRAPPING_NAME_PATTERN
support to XmlEncoderThis 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 ensures that fields named
firstname
orlastname
are always wrapped in CDATA tags, even if their content doesn't match the typical pattern used byCDATA_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.