Skip to content

Commit b5a242b

Browse files
committed
XmlData now supports (non-)serialization of AnyXml type
1 parent dcb6ea7 commit b5a242b

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

examples/xml/utils.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232

3333
import logging
34+
3435
logging.basicConfig(level=logging.DEBUG)
3536

3637
import uuid
@@ -39,16 +40,10 @@
3940
from pprint import pprint
4041

4142
from lxml import etree
43+
from lxml.builder import E
4244

43-
from spyne.model.primitive import Uuid
44-
from spyne.model.primitive import Unicode
45-
from spyne.model.primitive import String
46-
from spyne.model.primitive import Integer
47-
from spyne.model.primitive import Decimal
48-
from spyne.model.primitive import DateTime
49-
from spyne.model.complex import XmlData
50-
from spyne.model.complex import ComplexModel
51-
from spyne.model.complex import XmlAttribute
45+
from spyne import AnyXml, Uuid, Unicode, String, Integer, Decimal, DateTime, \
46+
XmlData, ComplexModel, XmlAttribute
5247

5348
from spyne.util.xml import get_schema_documents
5449
from spyne.util.xml import get_object_as_xml
@@ -85,6 +80,7 @@ class ProductEdition(ComplexModel):
8580

8681
id = XmlAttribute(Uuid)
8782
name = XmlData(Unicode)
83+
addtl = XmlData(AnyXml)
8884

8985

9086
class Product(ComplexModel):
@@ -129,11 +125,14 @@ class Product(ComplexModel):
129125
print(get_xml_as_object(doc, Foo))
130126
print()
131127

128+
# could be anything, really
129+
elt = E.tag(E.subtag("subdata"))
130+
132131
# XmlData example.
133132
print("Product output (illustrates XmlData):")
134133
product = Product(
135134
id=uuid.uuid4(),
136-
edition=ProductEdition(id=uuid.uuid4(), name='My edition')
135+
edition=ProductEdition(id=uuid.uuid4(), name='My edition', addtl=elt)
137136
)
138137

139138
print()

spyne/model/complex.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from spyne.model import Point, Unicode, PushBase, ModelBase
4444
from spyne.model._base import PSSM_VALUES, apply_pssm
4545
from spyne.model.primitive import NATIVE_MAP
46+
from spyne.model.primitive._base import AnyXml
4647

4748
from spyne.util import six, memoize, memoize_id, sanitize_args, \
4849
memoize_ignore_none
@@ -143,10 +144,13 @@ class XmlData(XmlModifier):
143144
@classmethod
144145
def marshall(cls, prot, name, value, parent_elt):
145146
if value is not None:
146-
if len(parent_elt) == 0:
147-
parent_elt.text = prot.to_bytes(cls.type, value)
147+
if issubclass(cls.type, AnyXml):
148+
parent_elt.append(value)
148149
else:
149-
parent_elt[-1].tail = prot.to_bytes(cls.type, value)
150+
if len(parent_elt) == 0:
151+
parent_elt.text = prot.to_bytes(cls.type, value)
152+
else:
153+
parent_elt[-1].tail = prot.to_bytes(cls.type, value)
150154

151155
@classmethod
152156
def get_type_name(cls):

spyne/protocol/cloth/to_parent.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,12 @@ def _write_members(self, ctx, cls, inst, parent, use_ns=None, **kwargs):
423423
sub_name = self._gen_sub_name(cls, attr, k, use_ns)
424424

425425
if issubclass(v, XmlData):
426-
subvalstr = self.to_unicode(v.type, subvalue)
427-
if subvalstr is not None:
428-
parent.write(subvalstr)
426+
if issubclass(v.type, AnyXml):
427+
parent.write(subvalue)
428+
else:
429+
subvalstr = self.to_unicode(v.type, subvalue)
430+
if subvalstr is not None:
431+
parent.write(subvalstr)
429432
continue
430433

431434
if subvalue is not None or attr.min_occurs > 0:

0 commit comments

Comments
 (0)