Skip to content

[Serializer] Fix the XML comments encoding #28156

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
Aug 19, 2018

Conversation

maidmaid
Copy link
Contributor

@maidmaid maidmaid commented Aug 8, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets /
License MIT
Doc PR /

When we decode a XML comment, we get ['#comment' => ' foo ']. But when we encode this same content, the result is not the expected one.

$encoder->encode(['#comment' => ' foo '], 'xml');
Expected:
<response>
    <!-- foo -->
</response>

Actual:
<response>
  <item key="#comment"> foo </item>
</response>

@nicolas-grekas
Copy link
Member

Which is the lowest branch where this applies? This should be the target branch.

@maidmaid
Copy link
Contributor Author

maidmaid commented Aug 8, 2018

2.8 is supposed to be the lowest branch, but before that, can you ensure me it's actually a bug and not a feature ?

@nicolas-grekas
Copy link
Member

I cannot. Why do you think it's supposed to work this way?
From the code, this would work: $data = array('#' => ' foo ');. Isn't it enough?

@nicolas-grekas nicolas-grekas added this to the 2.8 milestone Aug 8, 2018
@nicolas-grekas
Copy link
Member

From the code, this would work: $data = array('#' => ' foo ');.

Actually not, which means this is a new feature, now a bug fix, isn't it?
It should be done in a BC-safe way, not sure that's the case right now, is it?

@maidmaid
Copy link
Contributor Author

maidmaid commented Aug 8, 2018

From the code, this would work: $data = array('#' => ' foo ');.

Encoding ['#' => ' foo '] works as expected (generates the value of the current node), but encoding ['#comment' => ' foo '] doesn't work (is supposed to generate a comment node).

I meant : encoding of XML comment wasn't handled before this PR. It's why we can see that as a new feature. On the other hand, the encoding and the decoding of a XML comment behave differently, it's why we also can see that as a bug.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Oh, ok for a bug fix then on my side. Here are some more random comments :)

@@ -445,7 +453,7 @@ private function needsCdataWrapping(string $val): bool
*
* @throws NotEncodableValueException
*/
private function selectNodeType(\DOMNode $node, $val): bool
private function selectNodeType(\DOMNode $node, $val, $key = null): bool
Copy link
Member

@nicolas-grekas nicolas-grekas Aug 15, 2018

Choose a reason for hiding this comment

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

why make it an optional argument? i.e.: shouldn't the call inside appendNode also pass the key?
otherside of the question: if the anwer is no, what about dealing with the #comment check in buildXml directly, adding } elseif ('#comment' === $key) { ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, dealing directly in buildXml() makes more sense because it's here that we manage the special cases like @, # and now like #comment.

@maidmaid maidmaid force-pushed the serializer-comment branch from 26f8e31 to caadb94 Compare August 15, 2018 20:58
@maidmaid maidmaid changed the base branch from master to 2.8 August 15, 2018 20:59
@maidmaid
Copy link
Contributor Author

As we said it's a bug fix, I rebased on 2.8.

@@ -235,6 +236,13 @@ public function getRootNodeName()
return false;
}

private function appendComment(\DOMNode $node, string $data): bool
Copy link
Member

Choose a reason for hiding this comment

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

Final protected like the others?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ! (Could you explain to me the goal of a final protected method please ?)

Copy link
Member

Choose a reason for hiding this comment

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

goal of a final protected method

allowing child classes to call the method but not override it.

@fabpot
Copy link
Member

fabpot commented Aug 16, 2018

I don't see a big fix here but a new feature.

@maidmaid
Copy link
Contributor Author

maidmaid commented Aug 16, 2018

@fabpot IMO, we should get an output equal to the input after decoding and then encoding it. Said differently, if this the condition below isn't respected, I think it's a bug :

$encoded === $encoder->encode($encoder->decode($encoded, $format), $format))

In this case, without this PR, the result it's like :

'<a><!-- b --></a>' === '<a><item key="#comment"> b </item></a>'

@maidmaid maidmaid force-pushed the serializer-comment branch 3 times, most recently from 09efffe to 477df1d Compare August 16, 2018 11:28
@fabpot
Copy link
Member

fabpot commented Aug 18, 2018

When the feature was added to the decoder, nobody thought about adding it to the decoder as well. That's sad we didn't think about this before. I still see it as a new feature (a missing one and a very welcome one for sure).

@maidmaid
Copy link
Contributor Author

Ok @fabpot, let's see it as a (missing) new feature. I'll soon rebase on master.

@maidmaid maidmaid force-pushed the serializer-comment branch from 477df1d to 0a1e599 Compare August 18, 2018 15:26
@maidmaid maidmaid changed the base branch from 2.8 to master August 18, 2018 15:26
@maidmaid maidmaid force-pushed the serializer-comment branch from 0a1e599 to 14c66fe Compare August 18, 2018 15:37
@maidmaid
Copy link
Contributor Author

Rebase done.

@fabpot
Copy link
Member

fabpot commented Aug 18, 2018

Can you add a note in the CHANGELOG of the component?

@maidmaid maidmaid force-pushed the serializer-comment branch from 14c66fe to d94a37f Compare August 19, 2018 10:41
@maidmaid
Copy link
Contributor Author

Note added.

@fabpot
Copy link
Member

fabpot commented Aug 19, 2018

Thank you @maidmaid.

@fabpot fabpot merged commit d94a37f into symfony:master Aug 19, 2018
fabpot added a commit that referenced this pull request Aug 19, 2018
This PR was merged into the 4.2-dev branch.

Discussion
----------

[Serializer] Fix the XML comments encoding

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | /
| License       | MIT
| Doc PR        | /

When we decode a XML comment, we get `['#comment' => ' foo ']`. But when we encode this same content, the result is not the expected one.

```php
$encoder->encode(['#comment' => ' foo '], 'xml');
```
```
Expected:
<response>
    <!-- foo -->
</response>

Actual:
<response>
  <item key="#comment"> foo </item>
</response>
```

Commits
-------

d94a37f Allow to encode xml comments
@maidmaid maidmaid deleted the serializer-comment branch August 27, 2018 22:05
This was referenced Nov 3, 2018
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.

4 participants