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
Do not append missing XML declaration in formatted responses #1183
Conversation
httpie/output/formatters/xml.py
Outdated
@@ -43,17 +43,22 @@ def format_body(self, body: str, mime: str): | |||
|
|||
from xml.parsers.expat import ExpatError | |||
from defusedxml.common import DefusedXmlException | |||
|
|||
pretty_body = body |
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.
I would do all the work into the else:
block, see my next comment.
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.
Yeah it would be more appropriate, i missed that part.
try: | ||
parsed_body = parse_xml(body) | ||
except ExpatError: | ||
pass # Invalid XML, ignore. | ||
except DefusedXmlException: | ||
pass # Unsafe XML, ignore. | ||
else: | ||
body = pretty_xml(parsed_body, |
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.
Perhaps something less invasive could be used? An idea:
try: ...
else:
has_declaration = body.startswith('<?xml version')
body = pretty_xml(parsed_body,
encoding=parsed_body.encoding,
indent=self.format_options['xml']['indent'],
standalone=parsed_body.standalone)
if not has_declaration:
body = body[body.index('\n') + 1:]
return body
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 seems more intuitive. I was not aware about the ordering of prolog attributes matter. so i choose to find out what is the first tag in original xml document.
But i tried one test case:
If unformatted file is this
<?xml version="1.0"?>
<root><test>testValue</test></root>
what would be expected, this
<?xml version="1.0"?>
<root>
<test>testValue</test>
</root>
or
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>testValue</test>
</root>
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.
xmlminidom
will set the encoding. I think it is good as-is.
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.
Sorry for the previous answer. I did a mistake.
HTTPie should not alter the response content.
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.
Oh okay, sorry been busy with personal work will try to fix that as well.
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.
@BoboTiG addressed the above test case as well.
But assuming if test case file is:
<root><test>testValue</test></root>
Output will be
<?xml version="1.0" ?>
<root>
<test>testValue</test>
</root>
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.
Given that input, the output should be:
<root>
<test>testValue</test>
</root>
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.
Could you add tests for different cases also 🙏 ?
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.
Sure I will add them.
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.
Not Adding test case
<root><test>testValue</test></root>
because it can be addressed by simple_raw.xml
Co-authored-by: Mickaël Schoentgen <contact@tiger-222.fr>
https://github.com/httpie/httpie/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22+ @BoboTiG Any other issues i could take a look at? |
Let's see with @jakubroztocil. |
@this-is-r-gaurav thanks for the PR, but we decided to with another way (#1227). |
Fixes #1156.