From 2872c937a398d1ab4011cd31b37075a4354c521f Mon Sep 17 00:00:00 2001 From: Christodoulos Tsoulloftas Date: Sun, 28 Apr 2024 09:12:03 +0300 Subject: [PATCH] fix: Allow short closed elements --- tests/formats/dataclass/serializers/tree/test_lxml.py | 3 ++- tests/formats/dataclass/serializers/tree/test_native.py | 3 ++- xsdata/formats/dataclass/serializers/tree/mixins.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/formats/dataclass/serializers/tree/test_lxml.py b/tests/formats/dataclass/serializers/tree/test_lxml.py index 2d820e64d..e9f4fa3df 100644 --- a/tests/formats/dataclass/serializers/tree/test_lxml.py +++ b/tests/formats/dataclass/serializers/tree/test_lxml.py @@ -9,6 +9,7 @@ class LxmlTreeSerializerTests(TestCase): def test_render(self): serializer = LxmlTreeSerializer() + books.book[0].genre = "" result = serializer.render(books) etree.indent(result) @@ -18,7 +19,7 @@ def test_render(self): ' \n' " Hightower, Kim\n" " The First Book\n" - " Fiction\n" + " \n" " 44.95\n" " 2000-10-01\n" " An amazing story of nothing.\n" diff --git a/tests/formats/dataclass/serializers/tree/test_native.py b/tests/formats/dataclass/serializers/tree/test_native.py index 5337bb6d1..739356831 100644 --- a/tests/formats/dataclass/serializers/tree/test_native.py +++ b/tests/formats/dataclass/serializers/tree/test_native.py @@ -12,6 +12,7 @@ class XmlTreeSerializerTests(TestCase): @pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9") def test_render(self): serializer = XmlTreeSerializer() + books.book[0].genre = "" result = serializer.render(books) ElementTree.indent(result) @@ -21,7 +22,7 @@ def test_render(self): ' \n' " Hightower, Kim\n" " The First Book\n" - " Fiction\n" + " \n" " 44.95\n" " 2000-10-01\n" " An amazing story of nothing.\n" diff --git a/xsdata/formats/dataclass/serializers/tree/mixins.py b/xsdata/formats/dataclass/serializers/tree/mixins.py index 70e2d7c79..ff5c9efd2 100644 --- a/xsdata/formats/dataclass/serializers/tree/mixins.py +++ b/xsdata/formats/dataclass/serializers/tree/mixins.py @@ -55,7 +55,8 @@ def build(self, obj: T, builder: TreeBuilder): builder.end(*element) elif event == XmlWriterEvent.DATA: data = self.encode_data(element[0]) - builder.data(data) + if data: + builder.data(data) else: raise XmlHandlerError(f"Unhandled event: `{event}`.")