Skip to content

Commit ca84a40

Browse files
committed
Guess the serialization format AnyType.
Instead of passing the value as-is to lxml the format is now serialized based on the instance type. Fixes #552
1 parent c09bb53 commit ca84a40

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/zeep/xsd/types/any.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,22 @@ def resolve(self):
9494
return self
9595

9696
def xmlvalue(self, value):
97-
return value
97+
"""Guess the xsd:type for the value and use corresponding serializer"""
98+
from zeep.xsd.types import builtins
99+
100+
available_types = [
101+
builtins.String,
102+
builtins.Boolean,
103+
builtins.Decimal,
104+
builtins.Float,
105+
builtins.DateTime,
106+
builtins.Date,
107+
builtins.Time,
108+
]
109+
for xsd_type in available_types:
110+
if isinstance(value, xsd_type.accepted_types):
111+
return xsd_type().xmlvalue(value)
112+
return str(value)
98113

99114
def pythonvalue(self, value, schema=None):
100115
return value

tests/test_xsd_any.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,20 @@ def test_element_any_type():
256256
schema = xsd.Schema(node)
257257

258258
container_elm = schema.get_element('{http://tests.python-zeep.org/}container')
259-
obj = container_elm(something='bar')
259+
obj = container_elm(something=datetime.time(18, 29, 59))
260260

261261
node = etree.Element('document')
262262
container_elm.render(node, obj)
263263
expected = """
264264
<document>
265265
<ns0:container xmlns:ns0="http://tests.python-zeep.org/">
266-
<ns0:something>bar</ns0:something>
266+
<ns0:something>18:29:59</ns0:something>
267267
</ns0:container>
268268
</document>
269269
"""
270270
assert_nodes_equal(expected, node)
271271
item = container_elm.parse(node.getchildren()[0], schema)
272-
assert item.something == 'bar'
272+
assert item.something == '18:29:59'
273273

274274

275275
def test_element_any_type_unknown_type():

0 commit comments

Comments
 (0)