From 7176cfc9abbd53a99694645d2b7a627fe25d3be3 Mon Sep 17 00:00:00 2001 From: Sebastian Wozniak Date: Wed, 26 Nov 2025 13:16:30 +0100 Subject: [PATCH] fix: preserve attribute values when extending complexType When a complexType with only attributes was extended by another complexType, the attributes were being rendered as "NotSet" instead of their actual values. This occurred because a placeholder element was unnecessarily created during type extension, causing the base type to be rendered twice - once with correct values and once with empty values that overwrote the correct ones. Fixes #991 --- src/zeep/xsd/types/complex.py | 2 + tests/test_xsd_complex_types.py | 104 ++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/src/zeep/xsd/types/complex.py b/src/zeep/xsd/types/complex.py index 4c69a9cde..65c39b953 100644 --- a/src/zeep/xsd/types/complex.py +++ b/src/zeep/xsd/types/complex.py @@ -447,6 +447,8 @@ def extend(self, base): elif self._element or base_element: element = self._element or base_element + elif isinstance(base, ComplexType): + element = None else: element = Element("_value_1", base) diff --git a/tests/test_xsd_complex_types.py b/tests/test_xsd_complex_types.py index 55430c91f..6efb8945b 100644 --- a/tests/test_xsd_complex_types.py +++ b/tests/test_xsd_complex_types.py @@ -422,3 +422,107 @@ def test_ignore_sequence_order(): response = elm.parse(node[0], schema) assert response.Baz.id == 3 + + +def test_extension_with_attributes_preserves_values(): + schema = xsd.Schema( + load_xml( + """ + + + + + + + + + + + + + + """ + ) + ) + schema.set_ns_prefix("tns", "http://tests.python-zeep.org/") + + item_elm = schema.get_element("tns:item") + obj = item_elm(attr1=10, attr2="test") + + assert obj.attr1 == 10 + assert obj.attr2 == "test" + + result = render_node(item_elm, obj) + expected = """ + + + + """ + assert_nodes_equal(result, expected) + + parsed = item_elm.parse(result[0], schema) + assert parsed.attr1 == 10 + assert parsed.attr2 == "test" + + +def test_extension_with_attributes_and_elements(): + schema = xsd.Schema( + load_xml( + """ + + + + + + + + + + + + + + + + + + + + + + + + """ + ) + ) + schema.set_ns_prefix("tns", "http://tests.python-zeep.org/") + + container_elm = schema.get_element("tns:container") + extended_type = schema.get_type("tns:ExtendedType") + + obj = container_elm(item=extended_type(attr1=10, attr2="test")) + + assert obj.item.attr1 == 10 + assert obj.item.attr2 == "test" + + result = render_node(container_elm, obj) + expected = """ + + + + + + """ + assert_nodes_equal(result, expected) + + parsed = container_elm.parse(result[0], schema) + assert parsed.item.attr1 == 10 + assert parsed.item.attr2 == "test"