From 827210be0df1be7e8081d39a6e5a14c750deaa89 Mon Sep 17 00:00:00 2001 From: Chris Tsou Date: Sun, 3 Nov 2024 08:50:31 +0200 Subject: [PATCH] fix: Avoid conflict with attributes named value when flattening extensions (#1085) * fix: Avoid conflict with attributes named value when flattening extensions * chore: Update pre-commit hooks --- .pre-commit-config.yaml | 6 +++--- .../codegen/handlers/test_flatten_class_extensions.py | 10 ++++++++++ xsdata/codegen/handlers/flatten_class_extensions.py | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1117e8f68..01cd0c282 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,18 +8,18 @@ repos: - id: end-of-file-fixer - id: debug-statements - repo: https://github.com/crate-ci/typos - rev: v1.26.0 + rev: v1.27.0 hooks: - id: typos exclude: ^tests/|.xsd|xsdata/models/datatype.py$ - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.7.0 + rev: v0.7.2 hooks: - id: ruff args: [ --fix, --show-fixes] - id: ruff-format - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.12.1 + rev: v1.13.0 hooks: - id: mypy files: ^(xsdata/) diff --git a/tests/codegen/handlers/test_flatten_class_extensions.py b/tests/codegen/handlers/test_flatten_class_extensions.py index ac2154428..f055dba81 100644 --- a/tests/codegen/handlers/test_flatten_class_extensions.py +++ b/tests/codegen/handlers/test_flatten_class_extensions.py @@ -474,3 +474,13 @@ def test_add_default_attribute_with_any_type(self): self.assertEqual(0, len(item.extensions)) self.assertEqual(expected, item.attrs[0]) self.assertEqual(expected.restrictions, item.attrs[0].restrictions) + + def test_get_or_create_attribute_ignore_existing_attribute(self): + target = ClassFactory.create() + attr = AttrFactory.create(tag=Tag.ATTRIBUTE, name="value") + + result = FlattenClassExtensions.get_or_create_attribute( + target, "value", Tag.EXTENSION + ) + + self.assertIsNot(attr.tag, result) diff --git a/xsdata/codegen/handlers/flatten_class_extensions.py b/xsdata/codegen/handlers/flatten_class_extensions.py index 4b792f917..31d06d459 100644 --- a/xsdata/codegen/handlers/flatten_class_extensions.py +++ b/xsdata/codegen/handlers/flatten_class_extensions.py @@ -380,7 +380,7 @@ def get_or_create_attribute(cls, target: Class, name: str, tag: str) -> Attr: tag: The attr tag name """ attr = ClassUtils.find_attr(target, name) - if attr is None: + if attr is None or attr.tag == Tag.ATTRIBUTE: attr = Attr(name=name, tag=tag) attr.restrictions.min_occurs = 1 attr.restrictions.max_occurs = 1