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: Resolve attr name collisions with unusual names #991

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading