-
-
Notifications
You must be signed in to change notification settings - Fork 32
fix: escape XML special characters in @attrs values #242
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
Conversation
XML attribute values containing special characters (<, >, &, ", ') were not being properly escaped, resulting in invalid XML output. Changes: - Update make_attrstring() to call escape_xml() on attribute values - Add comprehensive tests for attribute escaping scenarios - Ensure backward compatibility with existing functionality Before: <Info HelpText="spec version <here>" /> After: <Info HelpText="spec version <here>" /> Resolves issue where @attrs dictionary values were output as raw text instead of properly escaped XML attribute values.
Reviewer's GuideIntegrates XML escaping into attribute serialization and expands the test suite with comprehensive scenarios and edge-case coverage. File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #242 +/- ##
=======================================
Coverage 99.30% 99.30%
=======================================
Files 3 3
Lines 288 288
=======================================
Hits 286 286
Misses 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
Hey @vinitkumar - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `tests/test_dict2xml.py:1109` </location>
<code_context>
+ # Verify the element content is also properly escaped
+ assert ">content<" in result
+
+ def test_attrs_empty_and_none_values(self) -> None:
+ """Test attribute handling with empty and None values."""
+ data = {
+ 'Element': {
+ "@attrs": {
+ "empty": "",
+ "zero": 0,
+ "false": False
+ }
+ }
+ }
+ result = dicttoxml.dicttoxml(data, attr_type=False, item_wrap=False, root=False).decode('utf-8')
+
+ assert 'empty=""' in result
+ assert 'zero="0"' in result
+ assert 'false="False"' in result
+
+ def test_make_attrstring_function_directly(self) -> None:
</code_context>
<issue_to_address>
Consider testing `None` as an attribute value.
Including a test for `None` as an attribute value will help ensure its handling is explicitly verified and prevent future regressions.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
data = {
'Element': {
"@attrs": {
"empty": "",
"zero": 0,
"false": False
}
}
}
result = dicttoxml.dicttoxml(data, attr_type=False, item_wrap=False, root=False).decode('utf-8')
assert 'empty=""' in result
assert 'zero="0"' in result
assert 'false="False"' in result
=======
data = {
'Element': {
"@attrs": {
"empty": "",
"zero": 0,
"false": False,
"none": None
}
}
}
result = dicttoxml.dicttoxml(data, attr_type=False, item_wrap=False, root=False).decode('utf-8')
assert 'empty=""' in result
assert 'zero="0"' in result
assert 'false="False"' in result
assert 'none="None"' in result
>>>>>>> REPLACE
</suggested_fix>
### Comment 2
<location> `tests/test_dict2xml.py:1136` </location>
<code_context>
+ "ampersand": "Tom & Jerry",
+ "quotes": 'Say "hello"'
+ }
+ result = make_attrstring(attrs)
+
+ assert 'test="value <here>"' in result
+ assert 'ampersand="Tom & Jerry"' in result
+ assert 'quotes="Say "hello""' in result
+
+ # Test empty attributes
</code_context>
<issue_to_address>
Use an exact match assertion for `make_attrstring` test.
Since the output is fully predictable, use an exact equality assertion (e.g., `assert result == expected`) to make the test more precise.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
result = make_attrstring(attrs)
assert 'test="value <here>"' in result
assert 'ampersand="Tom & Jerry"' in result
assert 'quotes="Say "hello""' in result
=======
result = make_attrstring(attrs)
expected = 'test="value <here>" ampersand="Tom & Jerry" quotes="Say "hello""'
assert result == expected
>>>>>>> REPLACE
</suggested_fix>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| data = { | ||
| 'Element': { | ||
| "@attrs": { | ||
| "empty": "", | ||
| "zero": 0, | ||
| "false": False | ||
| } | ||
| } | ||
| } | ||
| result = dicttoxml.dicttoxml(data, attr_type=False, item_wrap=False, root=False).decode('utf-8') | ||
|
|
||
| assert 'empty=""' in result | ||
| assert 'zero="0"' in result | ||
| assert 'false="False"' in result |
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.
suggestion (testing): Consider testing None as an attribute value.
Including a test for None as an attribute value will help ensure its handling is explicitly verified and prevent future regressions.
| data = { | |
| 'Element': { | |
| "@attrs": { | |
| "empty": "", | |
| "zero": 0, | |
| "false": False | |
| } | |
| } | |
| } | |
| result = dicttoxml.dicttoxml(data, attr_type=False, item_wrap=False, root=False).decode('utf-8') | |
| assert 'empty=""' in result | |
| assert 'zero="0"' in result | |
| assert 'false="False"' in result | |
| data = { | |
| 'Element': { | |
| "@attrs": { | |
| "empty": "", | |
| "zero": 0, | |
| "false": False, | |
| "none": None | |
| } | |
| } | |
| } | |
| result = dicttoxml.dicttoxml(data, attr_type=False, item_wrap=False, root=False).decode('utf-8') | |
| assert 'empty=""' in result | |
| assert 'zero="0"' in result | |
| assert 'false="False"' in result | |
| assert 'none="None"' in result |
| result = make_attrstring(attrs) | ||
|
|
||
| assert 'test="value <here>"' in result | ||
| assert 'ampersand="Tom & Jerry"' in result | ||
| assert 'quotes="Say "hello""' in result |
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.
suggestion (testing): Use an exact match assertion for make_attrstring test.
Since the output is fully predictable, use an exact equality assertion (e.g., assert result == expected) to make the test more precise.
| result = make_attrstring(attrs) | |
| assert 'test="value <here>"' in result | |
| assert 'ampersand="Tom & Jerry"' in result | |
| assert 'quotes="Say "hello""' in result | |
| result = make_attrstring(attrs) | |
| expected = 'test="value <here>" ampersand="Tom & Jerry" quotes="Say "hello""' | |
| assert result == expected |
XML attribute values containing special characters (<, >, &, ", ') were not being properly escaped, resulting in invalid XML output.
Changes:
Before:
After:
Resolves issue where @attrs dictionary values were output as raw text instead of properly escaped XML attribute values.
Fixes #199
Summary by Sourcery
Ensure XML attribute values are properly escaped to avoid invalid output
Bug Fixes:
Tests: