Skip to content

Commit

Permalink
fix: Resolve attr name collisions with unusual names (#991)
Browse files Browse the repository at this point in the history
  • Loading branch information
tefra authored Mar 22, 2024
1 parent 27c62e6 commit 6820aa1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/codegen/models/test_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_property_qname(self):
attr = AttrFactory.attribute(name="a", namespace="b")
self.assertEqual("{b}a", attr.qname)

def test_property_is_property(self):
def test_property_is_attribute(self):
self.assertTrue(AttrFactory.attribute().is_attribute)
self.assertTrue(AttrFactory.any_attribute().is_attribute)
self.assertFalse(AttrFactory.element().is_attribute)
Expand Down
4 changes: 4 additions & 0 deletions tests/codegen/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ def test_rename_duplicate_attributes(self):
AttrFactory.create(name="g[A]", tag=Tag.ENUMERATION),
AttrFactory.create(name="g_a", tag=Tag.ENUMERATION),
AttrFactory.create(name="g_a_1", tag=Tag.ENUMERATION),
AttrFactory.create(name="%", tag=Tag.ENUMERATION),
AttrFactory.create(name="$", tag=Tag.ENUMERATION),
]
target = ClassFactory.create(attrs=attrs)

Expand All @@ -344,6 +346,8 @@ def test_rename_duplicate_attributes(self):
"g[A]_2",
"g_a_3",
"g_a_1",
"%",
"$_1",
]
self.assertEqual(expected, [x.name for x in attrs])

Expand Down
5 changes: 4 additions & 1 deletion xsdata/codegen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from xsdata.models.enums import DataType, Tag
from xsdata.utils import collections, namespaces, text
from xsdata.utils.constants import DEFAULT_ATTR_NAME


class ClassUtils:
Expand Down Expand Up @@ -386,7 +387,9 @@ def merge_attributes(cls, target: Attr, source: Attr):
@classmethod
def rename_duplicate_attributes(cls, target: Class):
"""Find and rename attributes with the same slug."""
grouped = collections.group_by(target.attrs, key=get_slug)
grouped = collections.group_by(
target.attrs, key=lambda x: x.slug or DEFAULT_ATTR_NAME
)
for items in grouped.values():
total = len(items)
if total == 2 and not items[0].is_enumeration:
Expand Down

0 comments on commit 6820aa1

Please sign in to comment.