From 3bcd563125992fc5c22080cce1f9268c5b1440b7 Mon Sep 17 00:00:00 2001 From: Christodoulos Tsoulloftas Date: Fri, 22 Mar 2024 21:21:47 +0200 Subject: [PATCH] fix: Resolve attr name collisions with unusual names --- tests/codegen/models/test_attr.py | 2 +- tests/codegen/test_utils.py | 4 ++++ xsdata/codegen/utils.py | 5 ++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/codegen/models/test_attr.py b/tests/codegen/models/test_attr.py index 47788ee4f..7578e12eb 100644 --- a/tests/codegen/models/test_attr.py +++ b/tests/codegen/models/test_attr.py @@ -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) diff --git a/tests/codegen/test_utils.py b/tests/codegen/test_utils.py index d34540a21..288402277 100644 --- a/tests/codegen/test_utils.py +++ b/tests/codegen/test_utils.py @@ -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) @@ -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]) diff --git a/xsdata/codegen/utils.py b/xsdata/codegen/utils.py index 535b03afc..8d0e5bf95 100644 --- a/xsdata/codegen/utils.py +++ b/xsdata/codegen/utils.py @@ -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: @@ -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: