8000 [Serializer][XmlEncoder] Allow removing empty tags in generated XML by amoiraud · Pull Request #20524 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer][XmlEncoder] Allow removing empty tags in generated XML #20524

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
Closed
Show file tree
Hide file tree
Changes from 5 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
4 changes: 3 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
} elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
$append = $this->appendNode($parentNode, $data, 'item', $key);
} else {
$append = $this->appendNode($parentNode, $data, $key);
if ($data !== null || !isset($this->context['remove_empty_tags']) || $this->context['remove_empty_tags'] === false) {
Copy link
Member

Choose a reason for hiding this comment

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

should be elseif

Copy link
Member

Choose a reason for hiding this comment

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

and please also use a Yoda condition here: null !== $data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's done :

} elseif (null !== $data || !isset($this->context['remove_empty_tags']) || false === $this->context['remove_empty_tags']) {
    $append = $this->appendNode($parentNode, $data, $key);
}

$append = $this->appendNode($parentNode, $data, $key);
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ public function testEncodeXmlAttributes()
$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
}

public function testEncodeRemovingEmptyTags()
{
$array = array('person' => array('firstname' => 'Peter', 'lastname' => null));

$expected = '<?xml version="1.0"?>'."\n".
'<response><person><firstname>Peter</firstname></person></response>'."\n";

$context = array(
Copy link
Member

Choose a reason for hiding this comment

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

Can be inline

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right i have made the modification

'remove_empty_tags' => true,
);

$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
}

public function testEncodeNotRemovingEmptyTags()
{
$array = array('person' => array('firstname' => 'Peter', 'lastname' => null));

$expected = '<?xml version="1.0"?>'."\n".
'<response><person><firstname>Peter</firstname><lastname/></person></response>'."\n";

$this->assertSame($expected, $this->encoder->encode($array, 'xml'));
}

public function testContext()
{
$array = array('person' => array('name' => 'George Abitbol'));
Expand Down
0