Fix wrapper binding when collection item element name is reused - #1224
Merged
tefra merged 1 commit intoJul 10, 2026
Merged
Conversation
Two wrapped collections on the same dataclass that reuse the same item element name (e.g. both <Foos> and <Bars> contain <Property>) failed to parse. The wrapper node proxied the item to the parent by the bare item qname, so both build and bind matched the first field sharing that qname, routing every item into it and raising a TypeError for the required arguments of the mismatched item class. Scope the item lookup to the active wrapper qname: WrapperNode now knows its wrapper element and passes it down, so ElementNode filters candidate vars by their wrapper_qname when building child nodes and records the wrapper per item to disambiguate binding. Fixes tefra#1142.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1224 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 116 116
Lines 9376 9390 +14
Branches 1439 1443 +4
=========================================
+ Hits 9376 9390 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



📒 Description
Resolves #1142
Parsing a document with two wrapped collections on the same dataclass that reuse the same item element name failed. In the issue's example both
<Foos>and<Bars>wrap items named<Property>, and parsing raised:🔗 What I've Done
Root cause. A
WrapperNodeproxies the wrapped item to its parentElementNodeusing only the bare item qname (Property). Both the build step (ElementNode.child) and the bind step (ElementNode.bind_object) resolve the field viameta.find_children(qname), which returns every field sharing that qname in declaration order and picks the first workable one. So items from<Bars>were routed into thefoosfield, and the mismatched item class failed to construct.Fix. Scope the item lookup to the active wrapper qname:
WrapperNodenow knows its wrapper element qname and passes it down when proxying to the parent.ElementNode.childfilters candidate vars by theirwrapper_qnamewhen a wrapper is active, and records the wrapper per item qname (in document order).ElementNode.bind_objectpops the recorded wrapper for each parsed item and binds it to the field whosewrapper_qnamematches, so sibling wrappers reusing the same item name route to the correct field.The change is confined to
xsdata/formats/dataclass/parsersand leaves the non-wrapper code paths untouched (the wrapper argument defaults toNone).💬 Comments
Verified that serializing the same model already produced the expected XML, so this makes the parse/serialize round trip symmetric.
🛫 Checklist
Tests / Verification
WrapperTests.test_reused_item_namereproducing the issue. It fails onmainwith the reportedTypeErrorand passes with this fix.pytest tests/— 1091 passed, 17 skipped.ruff check/ruff format --checkclean on the changed files;mypyreports no new errors.Disclosure: prepared with AI assistance; reviewed and verified locally.