Skip to content

Commit

Permalink
fix: TreeSerializer start pending tag on all flush events" (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
tefra authored Apr 24, 2024
1 parent a2af584 commit 826e6f2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions tests/formats/dataclass/serializers/tree/test_lxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ def test_render(self):
actual = etree.tostring(result)
expected = (
'<ns0:books xmlns:ns0="urn:books">\n'
" <book>\n"
' <book id="bk001" lang="en">\n'
" <author>Hightower, Kim</author>\n"
" <title>The First Book</title>\n"
" <genre>Fiction</genre>\n"
" <price>44.95</price>\n"
" <pub_date>2000-10-01</pub_date>\n"
" <review>An amazing story of nothing.</review>\n"
" </book>\n"
" <book>\n"
' <book id="bk002" lang="en">\n'
" <author>Nagata, Suanne</author>\n"
" <title>Becoming Somebody</title>\n"
" <genre>Biography</genre>\n"
Expand Down
4 changes: 2 additions & 2 deletions tests/formats/dataclass/serializers/tree/test_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def test_render(self):
actual = ElementTree.tostring(result)
expected = (
'<ns0:books xmlns:ns0="urn:books">\n'
" <book>\n"
' <book id="bk001" lang="en">\n'
" <author>Hightower, Kim</author>\n"
" <title>The First Book</title>\n"
" <genre>Fiction</genre>\n"
" <price>44.95</price>\n"
" <pub_date>2000-10-01</pub_date>\n"
" <review>An amazing story of nothing.</review>\n"
" </book>\n"
" <book>\n"
' <book id="bk002" lang="en">\n'
" <author>Nagata, Suanne</author>\n"
" <title>Becoming Somebody</title>\n"
" <genre>Biography</genre>\n"
Expand Down
8 changes: 4 additions & 4 deletions xsdata/formats/dataclass/serializers/tree/mixins.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import abc
from dataclasses import dataclass
from typing import Any, Dict, Optional, Protocol
from typing import Any, Dict, Protocol

from xsdata.exceptions import XmlHandlerError
from xsdata.formats.converter import converter
from xsdata.formats.dataclass.serializers.mixins import EventGenerator, XmlWriterEvent
from xsdata.formats.types import T
from xsdata.models.enums import EventType


class TreeBuilder(Protocol):
Expand Down Expand Up @@ -39,8 +38,9 @@ def build(self, obj: T, builder: TreeBuilder):
"""
pending_tag = None
pending_attrs: Dict[str, Any] = {}
flush_events = (XmlWriterEvent.START, XmlWriterEvent.END, XmlWriterEvent.DATA)
for event, *element in self.generate(obj):
if pending_tag is not None:
if pending_tag is not None and event in flush_events:
builder.start(pending_tag, pending_attrs)
pending_tag = None
pending_attrs = {}
Expand All @@ -51,7 +51,7 @@ def build(self, obj: T, builder: TreeBuilder):
elif event == XmlWriterEvent.ATTR:
key, value = element
pending_attrs[key] = self.encode_data(value)
elif event == EventType.END:
elif event == XmlWriterEvent.END:
builder.end(*element)
elif event == XmlWriterEvent.DATA:
data = self.encode_data(element[0])
Expand Down

0 comments on commit 826e6f2

Please sign in to comment.