8000 [Serializer] Add xml context option to ignore empty attributes by qdequippe · Pull Request #58599 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add xml context option to ignore empty attributes #58599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and 8000 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
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
CHANGELOG
=========

7.3
---

* Deprecate the `CompiledClassMetadataFactory` and `CompiledClassMetadataCacheWarmer` classes
Copy link
Contributor

Choose a reason for hiding this comment

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

The changes in the CHANGELOG looks unrelated 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Forgot to mention that I fixed it in the next commit. My bad, sorry.


7.2
---

* Deprecate the `csv_escape_char` context option of `CsvEncoder` and the `CsvEncoder::ESCAPE_CHAR_KEY` constant
* Deprecate `CsvEncoderContextBuilder::withEscapeChar()` method
* Deprecate the `csv_escape_char` context option of `CsvEncoder`, the `CsvEncoder::ESCAPE_CHAR_KEY` constant
and the `CsvEncoderContextBuilder::withEscapeChar()` method, following its deprecation in PHP 8.4
* Add `SnakeCaseToCamelCaseNameConverter`
* Support subclasses of `\DateTime` and `\DateTimeImmutable` for denormalization
* Add the `UidNormalizer::NORMALIZATION_FORMAT_RFC9562` constant
Expand All @@ -19,7 +24,6 @@ CHANGELOG

* Add arguments `$class`, `$format` and `$context` to `NameConverterInterface::normalize()` and `NameConverterInterface::denormalize()`
* Add `DateTimeNormalizer::CAST_KEY` context option
* Add `Default` and "class name" default groups
* Add `AbstractNormalizer::FILTER_BOOL` context option
* Add `CamelCaseToSnakeCaseNameConverter::REQUIRE_SNAKE_CASE_PROPERTIES` context option
* Deprecate `AbstractNormalizerContextBuilder::withDefaultContructorArguments(?array $defaultContructorArguments)`, use `withDefaultConstructorArguments(?array $defaultConstructorArguments)` instead (note the missing `s` character in Contructor word in deprecated method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,12 @@ public function withCdataWrappingPattern(?string $cdataWrappingPattern): static
{
return $this->with(XmlEncoder::CDATA_WRAPPING_PATTERN, $cdataWrappingPattern);
}

/**
* Configures whether to ignore empty attributes.
*/
public function withIgnoreEmptyAttributes(?bool $ignoreEmptyAttributes): static
{
return $this->with(XmlEncoder::IGNORE_EMPTY_ATTRIBUTES, $ignoreEmptyAttributes);
}
}
9 changes: 9 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
public const VERSION = 'xml_version';
public const CDATA_WRAPPING = 'cdata_wrapping';
public const CDATA_WRAPPING_PATTERN = 'cdata_wrapping_pattern';
public const IGNORE_EMPTY_ATTRIBUTES = 'ignore_empty_attributes';

private array $defaultContext = [
self::AS_COLLECTION => false,
Expand All @@ -72,6 +73,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
self::TYPE_CAST_ATTRIBUTES => true,
self::CDATA_WRAPPING => true,
self::CDATA_WRAPPING_PATTERN => '/[<>&]/',
self::IGNORE_EMPTY_ATTRIBUTES => false,
];

public function __construct(array $defaultContext = [])
Expand Down Expand Up @@ -355,6 +357,13 @@ private function buildXml(\DOMNode $parentNode, mixed $data, string $format, arr
if (\is_bool($data)) {
$data = (int) $data;
}

if ($context[self::IGNORE_EMPTY_ATTRIBUTES] ?? $this->defaultContext[self::IGNORE_EMPTY_ATTRIBUTES]) {
if (null === $data || '' === $data) {
continue;
}
}

$parentNode->setAttribute($attributeName, $data);
} elseif ('#' === $key) {
$append = $this->selectNodeType($parentNode, $data, $format, $context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testWithers(array $values)
->withVersion($values[XmlEncoder::VERSION])
->withCdataWrapping($values[XmlEncoder::CDATA_WRAPPING])
->withCdataWrappingPattern($values[XmlEncoder::CDATA_WRAPPING_PATTERN])
->withIgnoreEmptyAttributes($values[XmlEncoder::IGNORE_EMPTY_ATTRIBUTES])
->toArray();

$this->assertSame($values, $context);
Expand All @@ -69,6 +70,7 @@ public static function withersDataProvider(): iterable
XmlEncoder::VERSION => '1.0',
XmlEncoder::CDATA_WRAPPING => false,
XmlEncoder::CDATA_WRAPPING_PATTERN => '/[<>&"\']/',
XmlEncoder::IGNORE_EMPTY_ATTRIBUTES => true,
]];

yield 'With null values' => [[
Expand All @@ -86,6 +88,7 @@ public static function withersDataProvider(): iterable
XmlEncoder::VERSION => null,
XmlEncoder::CDATA_WRAPPING => null,
XmlEncoder::CDATA_WRAPPING_PATTERN => null,
XmlEncoder::IGNORE_EMPTY_ATTRIBUTES => null,
]];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1004,4 +1004,17 @@ private function createXmlWithDateTimeField(): string
<response><foo dateTime="%s"/></response>
', $this->exampleDateTimeString);
}

public function testEncodeIgnoringEmptyAttribute()
{
$expected = <<<'XML'
<?xml version="1.0"?>
<response>Test</response>

XML;

$data = ['#' => 'Test', '@attribute' => '', '@attribute2' => null];

$this->assertEquals($expected, $this->encoder->encode($data, 'xml', ['ignore_empty_attributes' => true]));
}
}
Loading
0