Skip to content
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

verifyXml() escaping behaviour #466

Closed
JCook21 opened this issue Jan 25, 2024 · 7 comments
Closed

verifyXml() escaping behaviour #466

JCook21 opened this issue Jan 25, 2024 · 7 comments

Comments

@JCook21
Copy link

JCook21 commented Jan 25, 2024

I'm working with Jackson to generate some XML that I'd like to then approve with the verifyXml() method. Jackson takes care of escaping special characters in the XML but doesn't pretty print it. I'm finding some inconsistencies with how Approvals then formats the XML:

  • Single quotes in the DTD are being converted to double quotes. This isn't a major issue but is a difference between what Jackson outputs and how Approvals then formats it.
  • Emoji characters, which Jackson doesn't escape, are being escaped by Approvals. For example, "😼" is being output as 😼
  • XML special characters, which Jackson has already escaped, are being escaped again. For example, Jackson outputs & but Approvals then changes this to &amp.

Do you have any comments on this? Is it possible to just apply formatting to the XML string that adds line breaks and indents but without further formatting the content?

@isidore
Copy link
Member

isidore commented Feb 19, 2024

  1. would you be willing to join us on a zoom call to work on this?
  2. do you have a unit test we can look at to see the issues?

@JCook21
Copy link
Author

JCook21 commented Feb 21, 2024

Happy to join you on a Zoom call to look into this. I don't have a unit test yet but I can look to add one today.

@JCook21
Copy link
Author

JCook21 commented Feb 21, 2024

I'm wondering if a similar approach to JsonJacksonApprovals would work here too.

@JCook21
Copy link
Author

JCook21 commented Feb 21, 2024

Added a draft pull request #473 with a couple of tests to show the discrepancies I'm seeing. Strangely, I couldn't verify that & is being converted to &, even though I saw this in my own codebase a few weeks ago.

@LarsEckart
Copy link
Contributor

Please send an email to [email protected] and he'll forward you the invite for our next session. Looking forward to it!

@LarsEckart
Copy link
Contributor

hey Jeremy,

it's been a while but tonight we worked on it. It's tricky. A suggested workaround is to write this line of code:

public void verifyJacksonXml(String xml) {
    Approvals.verify(prettyPrintWithJackson(xml), new Options().forFile().withExtension(".xml"));
}

where you have to implement the prettyPrintWithJackson still yourself.

We'll get back to it in a week, if you want to join us, let us know.

@LarsEckart
Copy link
Contributor

hey @JCook21

we think we fixed this by using XOM. But we also created a seam so that you can pass your own xml pretty printer that Approvals will use.

If you get it working with jackson as the pretty printer, we'd love if you'd contribute it back! (we weren't able to when we tried) :)

We're publishing a new release now that contains XmlXomApprovals.verifyXml() and Approvals.verifyXml(xml, prettyPrinter)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants