Skip to content

Conversation

@vinitkumar
Copy link
Owner

@vinitkumar vinitkumar commented Jan 28, 2026

Summary by Sourcery

Add a testable, non-Python Rust core for json2xml and strengthen validation of its XML helper utilities.

New Features:

  • Expose core XML helper functions as a public Rust API independent of the Python binding.
  • Introduce a fuzzing crate with multiple fuzz targets for the Rust XML helper functions.

Enhancements:

  • Gate Python-specific Rust code behind an optional feature and add an rlib build to support non-Python consumers.

Build:

  • Update Rust crate configuration to support feature-flagged PyO3 usage and multiple crate types (cdylib, rlib).

CI:

  • Extend Rust CI to run unit tests with Python features disabled.

Documentation:

  • Refresh benchmark data, recommendations, and Zig project links in the performance documentation.

Tests:

  • Add comprehensive Rust unit tests for XML escaping, CDATA wrapping, XML name validation, and attribute string generation.
  • Seed and wire fuzzing corpora for the new fuzz targets to harden XML helper behavior.

vinitkumar and others added 5 commits January 28, 2026 16:03
- Make pyo3 dependency optional behind 'python' feature flag
- Add rlib crate-type to support both cdylib (Python) and rlib (fuzzing)
- Gate all Python-specific code with #[cfg(feature = "python")]
- Make pure Rust functions (escape_xml, wrap_cdata, etc.) public for fuzzing
- Add comprehensive unit tests for XML utility functions

Fixes linker error with Python 3.14 where PyUnicode_DATA and
PyUnicode_KIND symbols are now inline macros, not exported functions.
Fuzz targets can now build without linking against Python.

Amp-Thread-ID: https://ampcode.com/threads/T-019c0425-ac62-76d8-9d59-4d6aba3edf45
Co-authored-by: Amp <amp@ampcode.com>
Use more specific pattern matching (` key="`) instead of just checking
if key exists as substring. This avoids false positives with overlapping
keys (e.g. 'a' vs 'aa') or malformed attribute formatting.

Suggested-by: sourcery-ai
Amp-Thread-ID: https://ampcode.com/threads/T-019c0425-ac62-76d8-9d59-4d6aba3edf45
Co-authored-by: Amp <amp@ampcode.com>
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 28, 2026

Reviewer's Guide

Adds unit tests and fuzzing for core Rust XML helper functions, makes them public and buildable without the Python extension, introduces a Python feature flag in the Rust crate, wires Rust unit tests into CI, and refreshes benchmark documentation and references.

Class diagram for core Rust XML helpers, tests, and fuzz targets

classDiagram
    class json2xml_rs {
        +escape_xml(s: &str) String
        +wrap_cdata(s: &str) String
        +is_valid_xml_name(key: &str) bool
        +make_valid_xml_name(key: &str) (String, Option<(String, String)>)
        +make_attr_string(attrs: &[(String, String)]) String
    }

    class ConvertConfig {
        <<cfg_feature_python>>
        -attr_type: bool
        -cdata: bool
        -item_wrap: bool
        -list_headers: bool
    }

    class escape_xml_tests {
        <<test_module>>
        +escapes_ampersand()
        +escapes_double_quote()
        +escapes_single_quote()
        +escapes_less_than()
        +escapes_greater_than()
        +escapes_all_special_chars()
        +handles_empty_string()
        +handles_no_special_chars()
        +handles_unicode()
    }

    class wrap_cdata_tests {
        <<test_module>>
        +wraps_simple_string()
        +wraps_empty_string()
        +escapes_cdata_end_sequence()
        +handles_multiple_cdata_end_sequences()
        +handles_special_xml_chars()
    }

    class is_valid_xml_name_tests {
        <<test_module>>
        +accepts_simple_name()
        +accepts_name_with_underscore_prefix()
        +accepts_name_with_numbers()
        +accepts_name_with_hyphens()
        +accepts_name_with_dots()
        +accepts_name_with_colons()
        +rejects_empty_string()
        +rejects_name_starting_with_number()
        +rejects_name_starting_with_hyphen()
        +rejects_name_with_spaces()
        +rejects_xml_prefix_lowercase()
        +rejects_xml_prefix_uppercase()
        +rejects_xml_prefix_mixed_case()
    }

    class make_valid_xml_name_tests {
        <<test_module>>
        +returns_valid_name_unchanged()
        +prepends_n_to_numeric_key()
        +replaces_spaces_with_underscores()
        +falls_back_to_key_with_name_attr()
        +escapes_special_chars_in_name()
    }

    class make_attr_string_tests {
        <<test_module>>
        +returns_empty_for_empty_attrs()
        +formats_single_attr()
        +formats_multiple_attrs()
        +escapes_attr_values()
    }

    class fuzz_escape_xml {
        <<fuzz_target>>
        +run(input: String)
    }

    class fuzz_wrap_cdata {
        <<fuzz_target>>
        +run(input: String)
    }

    class fuzz_is_valid_xml_name {
        <<fuzz_target>>
        +run(input: String)
    }

    class fuzz_make_valid_xml_name {
        <<fuzz_target>>
        +run(input: String)
    }

    class fuzz_make_attr_string {
        <<fuzz_target>>
        +run(attrs: Vec<(String, String)>)
    }

    json2xml_rs <|.. ConvertConfig

    escape_xml_tests --> json2xml_rs : uses escape_xml
    wrap_cdata_tests --> json2xml_rs : uses wrap_cdata
    is_valid_xml_name_tests --> json2xml_rs : uses is_valid_xml_name
    make_valid_xml_name_tests --> json2xml_rs : uses make_valid_xml_name
    make_attr_string_tests --> json2xml_rs : uses make_attr_string

    fuzz_escape_xml --> json2xml_rs : uses escape_xml
    fuzz_wrap_cdata --> json2xml_rs : uses wrap_cdata
    fuzz_is_valid_xml_name --> json2xml_rs : uses is_valid_xml_name
    fuzz_make_valid_xml_name --> json2xml_rs : uses make_valid_xml_name
    fuzz_make_attr_string --> json2xml_rs : uses make_attr_string
Loading

File-Level Changes

Change Details Files
Expose core Rust helpers and gate Python integration behind a feature flag to enable testing and non-Python builds.
  • Mark escape_xml, wrap_cdata, is_valid_xml_name, make_valid_xml_name, and make_attr_string as pub so they can be used from tests and fuzzers.
  • Wrap PyO3 imports, ConvertConfig, and Python-facing conversion functions/pyfunctions/pymodule with cfg(feature = "python") so the crate can compile without Python support.
  • Introduce a default "python" feature that pulls in pyo3/extension-module while making the pyo3 dependency optional and allowing rlib builds.
rust/src/lib.rs
rust/Cargo.toml
Add targeted Rust unit tests for the core XML helper functions.
  • Add test modules for escaping, CDATA wrapping, XML name validation, XML name normalization, and attribute string construction, covering normal, edge, and Unicode cases.
  • Structure tests in nested modules for clarity and reuse of shared helpers from the parent module.
rust/src/lib.rs
Introduce a fuzzing workspace for the Rust crate and define fuzz targets for core helper functions.
  • Add rust/fuzz Cargo.toml configured for cargo-fuzz with json2xml_rs as a dependency without default features.
  • Create fuzz targets for escape_xml, wrap_cdata, is_valid_xml_name, make_valid_xml_name, and make_attr_string, including an Arbitrary-driven input struct for attributes and basic invariants for make_attr_string.
  • Check simple formatting invariants in fuzz_make_attr_string and include seed corpus files for escape_xml and make_attr_string to guide fuzzing.
rust/fuzz/Cargo.toml
rust/fuzz/fuzz_targets/fuzz_make_attr_string.rs
rust/fuzz/fuzz_targets/fuzz_escape_xml.rs
rust/fuzz/fuzz_targets/fuzz_is_valid_xml_name.rs
rust/fuzz/fuzz_targets/fuzz_make_valid_xml_name.rs
rust/fuzz/fuzz_targets/fuzz_wrap_cdata.rs
rust/fuzz/corpus/fuzz_escape_xml/*
rust/fuzz/corpus/fuzz_make_attr_string/*
Extend CI to run Rust unit tests without Python features enabled.
  • Add a cargo test --no-default-features step to the existing Rust CI workflow so core logic is verified without the python feature.
  • Keep existing clippy invocation using all-features for Python integration coverage.
.github/workflows/rust-ci.yml
Refresh and correct benchmark documentation and related project links.
  • Update benchmark date, dataset sizes (approximate), and timing tables for all implementations including new speedup factors.
  • Clarify CLI startup overhead numbers separately for Go and Zig and adjust narrative recommendations for which implementation to use per use case.
  • Fix Go and Zig implementation references to point at the v1.0.0 Go binary and the updated Zig repository under vinitkumar.
BENCHMARKS.md

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 Jan 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.89%. Comparing base (d346999) to head (cd76e36).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #271   +/-   ##
=======================================
  Coverage   95.89%   95.89%           
=======================================
  Files           5        5           
  Lines         463      463           
=======================================
  Hits          444      444           
  Misses         19       19           
Flag Coverage Δ
unittests 95.89% <ø> (ø)

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 - I've found 1 issue, and left some high level feedback:

  • The core helper functions (escape_xml, wrap_cdata, is_valid_xml_name, make_valid_xml_name, make_attr_string) are now pub, which expands the crate’s public API; consider using pub(crate) or gating them behind a feature (e.g. test/fuzzing) if they are not intended for external consumers.
  • The fuzz_make_attr_string target asserts that each key yields a {key}=" fragment in the result, which may be brittle for unusual keys (e.g. containing whitespace or characters that might be normalized/escaped); you may want to relax this invariant or derive expectations from the actual formatting/escaping rules used in make_attr_string to avoid false positives.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The core helper functions (`escape_xml`, `wrap_cdata`, `is_valid_xml_name`, `make_valid_xml_name`, `make_attr_string`) are now `pub`, which expands the crate’s public API; consider using `pub(crate)` or gating them behind a feature (e.g. `test`/`fuzzing`) if they are not intended for external consumers.
- The `fuzz_make_attr_string` target asserts that each key yields a ` {key}="` fragment in the result, which may be brittle for unusual keys (e.g. containing whitespace or characters that might be normalized/escaped); you may want to relax this invariant or derive expectations from the actual formatting/escaping rules used in `make_attr_string` to avoid false positives.

## Individual Comments

### Comment 1
<location> `rust/fuzz/fuzz_targets/fuzz_make_attr_string.rs:28-37` </location>
<code_context>
+        );
+    }
+    
+    // 4. Values should be escaped (no raw & < > " ' in values)
+    //    The make_attr_string calls escape_xml on values
+    
</code_context>

<issue_to_address>
**suggestion (testing):** The comment about escaped values is not enforced; consider adding an explicit check to strengthen the fuzz target.

Since the fuzz target doesn’t currently check that escaping actually happens, it won’t catch regressions in `escape_xml`. Consider adding an assertion (or a minimal parser) that inspects attribute values and verifies they contain no unescaped `&`, `<`, `>`, `"`, or `'` characters before concluding the output is valid.

```suggestion
    for (key, _value) in &input.attrs {
        let expected_fragment = format!(" {}=\"", key);
        assert!(
            result.contains(&expected_fragment),
            "Attribute fragment '{}' should appear in result '{}'",
            expected_fragment,
            result
        );
    }

    // Additionally, verify that attribute values are properly escaped:
    // - No raw <, >, " or ' characters may appear inside attribute values.
    // - Any '&' inside a value must be part of an entity (it must be followed
    //   by some characters and then a terminating ';' before the closing quote).
    for (key, _value) in &input.attrs {
        let expected_prefix = format!(" {}=\"", key);
        if let Some(start) = result.find(&expected_prefix) {
            let value_start = start + expected_prefix.len();
            if let Some(rel_end) = result[value_start..].find('"') {
                let value_end = value_start + rel_end;
                let value = &result[value_start..value_end];

                // 1. Forbid raw <, >, " and ' in attribute values.
                for forbidden in ['<', '>', '"', '\''] {
                    assert!(
                        !value.chars().any(|c| c == forbidden),
                        "Unescaped '{}' found in attribute value for key '{}' in '{}'",
                        forbidden,
                        key,
                        result
                    );
                }

                // 2. Ensure any '&' is part of something that at least looks like an entity:
                //    '&' must be followed by at least one non-';' character and then a ';'
                //    before the end of the value.
                let bytes = value.as_bytes();
                let mut i = 0;
                while i < bytes.len() {
                    if bytes[i] == b'&' {
                        // There must be at least one character after '&'
                        assert!(
                            i + 1 < bytes.len(),
                            "Dangling '&' at end of attribute value for key '{}' in '{}'",
                            key,
                            result
                        );

                        // Find the next ';' after '&'
                        let mut j = i + 1;
                        while j < bytes.len() && bytes[j] != b';' {
                            j += 1;
                        }

                        assert!(
                            j < bytes.len(),
                            "Found '&' in attribute value for key '{}' that is not terminated by ';' in '{}'",
                            key,
                            result
                        );

                        // Require at least one character between '&' and ';'
                        assert!(
                            j > i + 1,
                            "Empty entity reference '&;' in attribute value for key '{}' in '{}'",
                            key,
                            result
                        );

                        // Continue scanning after the ';'
                        i = j + 1;
                    } else {
                        i += 1;
                    }
                }
            }
        }
    }

```
</issue_to_address>

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 +28 to +37
for (key, _value) in &input.attrs {
let expected_fragment = format!(" {}=\"", key);
assert!(
result.contains(&expected_fragment),
"Attribute fragment '{}' should appear in result '{}'",
expected_fragment,
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): The comment about escaped values is not enforced; consider adding an explicit check to strengthen the fuzz target.

Since the fuzz target doesn’t currently check that escaping actually happens, it won’t catch regressions in escape_xml. Consider adding an assertion (or a minimal parser) that inspects attribute values and verifies they contain no unescaped &, <, >, ", or ' characters before concluding the output is valid.

Suggested change
for (key, _value) in &input.attrs {
let expected_fragment = format!(" {}=\"", key);
assert!(
result.contains(&expected_fragment),
"Attribute fragment '{}' should appear in result '{}'",
expected_fragment,
result
);
}
for (key, _value) in &input.attrs {
let expected_fragment = format!(" {}=\"", key);
assert!(
result.contains(&expected_fragment),
"Attribute fragment '{}' should appear in result '{}'",
expected_fragment,
result
);
}
// Additionally, verify that attribute values are properly escaped:
// - No raw <, >, " or ' characters may appear inside attribute values.
// - Any '&' inside a value must be part of an entity (it must be followed
// by some characters and then a terminating ';' before the closing quote).
for (key, _value) in &input.attrs {
let expected_prefix = format!(" {}=\"", key);
if let Some(start) = result.find(&expected_prefix) {
let value_start = start + expected_prefix.len();
if let Some(rel_end) = result[value_start..].find('"') {
let value_end = value_start + rel_end;
let value = &result[value_start..value_end];
// 1. Forbid raw <, >, " and ' in attribute values.
for forbidden in ['<', '>', '"', '\''] {
assert!(
!value.chars().any(|c| c == forbidden),
"Unescaped '{}' found in attribute value for key '{}' in '{}'",
forbidden,
key,
result
);
}
// 2. Ensure any '&' is part of something that at least looks like an entity:
// '&' must be followed by at least one non-';' character and then a ';'
// before the end of the value.
let bytes = value.as_bytes();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'&' {
// There must be at least one character after '&'
assert!(
i + 1 < bytes.len(),
"Dangling '&' at end of attribute value for key '{}' in '{}'",
key,
result
);
// Find the next ';' after '&'
let mut j = i + 1;
while j < bytes.len() && bytes[j] != b';' {
j += 1;
}
assert!(
j < bytes.len(),
"Found '&' in attribute value for key '{}' that is not terminated by ';' in '{}'",
key,
result
);
// Require at least one character between '&' and ';'
assert!(
j > i + 1,
"Empty entity reference '&;' in attribute value for key '{}' in '{}'",
key,
result
);
// Continue scanning after the ';'
i = j + 1;
} else {
i += 1;
}
}
}
}
}

pytest, pytest-cov, coverage, and setuptools were incorrectly listed
as runtime dependencies, causing them to be installed with
'pip install json2xml'. Moved them to [project.optional-dependencies]
under 'dev' group, so users get only defusedxml, urllib3, and xmltodict
at runtime. Developers can install with 'pip install json2xml[dev]'.

fixes #272
@vinitkumar vinitkumar merged commit 7386087 into master Jan 29, 2026
62 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.

2 participants