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

Fix/199 attr escape #222

Closed
wants to merge 4 commits into from
Closed

Fix/199 attr escape #222

wants to merge 4 commits into from

Conversation

vinitkumar
Copy link
Owner

@vinitkumar vinitkumar commented Dec 13, 2024

Fixes #199

Summary by Sourcery

Fix attribute escaping in XML conversion to handle special characters and add corresponding tests to ensure correct functionality.

Bug Fixes:

  • Fix attribute escaping in XML conversion to handle special characters like angle brackets, quotes, and ampersands.

Tests:

  • Add tests to verify the correct escaping of special characters in XML attributes, including angle brackets, quotes, ampersands, and mixed special characters.

Following will be escaped from now on

- angle brackets
- quotes
- ampersands
- and a mix of them too
@vinitkumar vinitkumar self-assigned this Dec 13, 2024
Copy link

sourcery-ai bot commented Dec 13, 2024

Reviewer's Guide by Sourcery

This PR implements XML attribute value escaping in the json2xml package. The implementation modifies the attribute string generation to properly escape special characters like angle brackets, quotes, and ampersands in XML attributes.

Class diagram for updated make_attrstring function

classDiagram
    class DictToXml {
        +make_attrstring(attr: dict[str, Any]) str
    }
    class EscapeUtils {
        +escape_xml(value: str) str
    }
    DictToXml --> EscapeUtils : uses
Loading

File-Level Changes

Change Details Files
Added XML attribute value escaping in attribute string generation
  • Modified attribute string generation to use escape_xml() function for attribute values
  • Ensures special characters (<, >, ", &) are properly escaped in XML output
json2xml/dicttoxml.py
Added comprehensive test cases for XML attribute escaping
  • Added test for escaping angle brackets
  • Added test for escaping quote characters
  • Added test for escaping ampersands
  • Added test for escaping mixed special characters
tests/test_json2xml.py

Assessment against linked issues

Issue Objective Addressed Explanation
#199 Implement XML escaping for special characters in @attrs values

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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a 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. You can also use
    this command to specify where the summary should be inserted.

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

Copy link

codecov bot commented Dec 13, 2024

❌ 4 Tests Failed:

Tests completed Failed Passed Skipped
89 4 85 0
View the top 3 failed tests by shortest run time
tests.test_json2xml.TestJson2xml::test_escapes_mixed_special_chars
Stack Traces | 0.009s run time
self = <tests.test_json2xml.TestJson2xml object at 0x000001fe3b0152b8>

    def test_escapes_mixed_special_chars(self):
        json_data = json.dumps({"root": {"@attrs": {"Text": "<tag> & \"quote\""}}})
        result = json2xml.Json2xml(json_data).to_xml()
>       assert '"Text": "&lt;tag&gt; &amp; \\"quote\\""' in result
E       assert '"Text": "&lt;tag&gt; &amp; \\"quote\\""' in '<?xml version="1.0" encoding="UTF-8"?>\n<all>\n\t<item type="str">{&quot;root&quot;: {&quot;@attrs&quot;: {&quot;Text&quot;: &quot;&lt;tag&gt; &amp; \\&quot;quote\\&quot;&quot;}}}</item>\n</all>\n'

tests\test_json2xml.py:250: AssertionError
tests.test_json2xml.TestJson2xml::test_escapes_quotes
Stack Traces | 0.009s run time
self = <tests.test_json2xml.TestJson2xml object at 0x000001fe3affa368>

    def test_escapes_quotes(self):
        json_data = json.dumps({"root": {"@attrs": {"Text": "\"quoted\""}}})
        result = json2xml.Json2xml(json_data).to_xml()
>       assert '"Text": "\\"quoted\\""' in result
E       assert '"Text": "\\"quoted\\""' in '<?xml version="1.0" encoding="UTF-8"?>\n<all>\n\t<item type="str">{&quot;root&quot;: {&quot;@attrs&quot;: {&quot;Text&quot;: &quot;\\&quot;quoted\\&quot;&quot;}}}</item>\n</all>\n'

tests\test_json2xml.py:240: AssertionError
tests.test_json2xml.TestJson2xml::test_escapes_angle_brackets
Stack Traces | 0.01s run time
self = <tests.test_json2xml.TestJson2xml object at 0x000001fe3affa4b8>

    def test_escapes_angle_brackets(self):
        json_data = json.dumps({"root": {"@attrs": {"HelpText": "version <here>"}}})
        result = json2xml.Json2xml(json_data).to_xml()
>       assert '"HelpText": "version &lt;here&gt;"' in result
E       assert '"HelpText": "version &lt;here&gt;"' in '<?xml version="1.0" encoding="UTF-8"?>\n<all>\n\t<item type="str">{&quot;root&quot;: {&quot;@attrs&quot;: {&quot;HelpText&quot;: &quot;version &lt;here&gt;&quot;}}}</item>\n</all>\n'

tests\test_json2xml.py:235: AssertionError

To view more test analytics, go to the Test Analytics Dashboard
📢 Thoughts on this report? Let us know!

Copy link

@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!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

@vinitkumar vinitkumar closed this Dec 18, 2024
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
1 participant