Skip to content

Conversation

@vinitkumar
Copy link
Owner

@vinitkumar vinitkumar commented Jun 12, 2025

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:
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:

  • Escape XML special characters in @attrs values when generating XML attributes

Tests:

  • Add comprehensive tests for XML attribute escaping covering special characters, empty, zero and None values and direct make_attrstring behavior

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 &lt;here&gt;" />

Resolves issue where @attrs dictionary values were output as raw text
instead of properly escaped XML attribute values.
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 12, 2025

Reviewer's Guide

Integrates XML escaping into attribute serialization and expands the test suite with comprehensive scenarios and edge-case coverage.

File-Level Changes

Change Details Files
Escape XML special characters in attribute values
  • Wrap attribute values with escape_xml in make_attrstring
json2xml/dicttoxml.py
Expand tests for attribute escaping and edge cases
  • Add test for basic attribute value escaping
  • Add tests covering all XML special characters in attributes
  • Add tests for empty, numeric, and boolean attribute values
  • Add direct tests for make_attrstring function behavior
tests/test_dict2xml.py

Assessment against linked issues

Issue Objective Addressed Explanation
#199 Ensure that special characters (<, >, &, ", ') in the values of @attrs are properly escaped in the generated XML.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov
Copy link

codecov bot commented Jun 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.30%. Comparing base (6cbdea8) to head (abf5011).
⚠️ Report is 11 commits behind head on master.

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           
Flag Coverage Δ
unittests 99.30% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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 &lt;here&gt;"' in result
+        assert 'ampersand="Tom &amp; Jerry"' in result
+        assert 'quotes="Say &quot;hello&quot;"' 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 &lt;here&gt;"' in result
        assert 'ampersand="Tom &amp; Jerry"' in result
        assert 'quotes="Say &quot;hello&quot;"' in result
=======
        result = make_attrstring(attrs)

        expected = 'test="value &lt;here&gt;" ampersand="Tom &amp; Jerry" quotes="Say &quot;hello&quot;"'
        assert result == expected
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 1111 to 1124
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
Copy link
Contributor

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.

Suggested change
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

Comment on lines 1136 to 1140
result = make_attrstring(attrs)

assert 'test="value &lt;here&gt;"' in result
assert 'ampersand="Tom &amp; Jerry"' in result
assert 'quotes="Say &quot;hello&quot;"' in result
Copy link
Contributor

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.

Suggested change
result = make_attrstring(attrs)
assert 'test="value &lt;here&gt;"' in result
assert 'ampersand="Tom &amp; Jerry"' in result
assert 'quotes="Say &quot;hello&quot;"' in result
result = make_attrstring(attrs)
expected = 'test="value &lt;here&gt;" ampersand="Tom &amp; Jerry" quotes="Say &quot;hello&quot;"'
assert result == expected

@vinitkumar vinitkumar merged commit df8903a into master Jun 12, 2025
31 checks passed
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

Successfully merging this pull request may close these issues.

dicttoxml - @attrs not escaping key:value pairs at all

2 participants