Skip to content

Commit e4a12d8

Browse files
committed
Improve support for inline schema's (#686, #663)
1 parent 760028e commit e4a12d8

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/zeep/xsd/elements/any.py

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ def parse(self, xmlelement, schema, context=None):
5858
if context_schema.documents.has_schema_document_for_ns(qname.namespace):
5959
schema = context_schema
6060
break
61+
else:
62+
# Try to parse the any result by iterating all the schemas
63+
for context_schema in context.schemas:
64+
try:
65+
data = context_schema.deserialize(xmlelement.getchildren()[0])
66+
return data
67+
except LookupError:
68+
continue
6169

6270
# Lookup type via xsi:type attribute
6371
xsd_type = qname_attr(xmlelement, xsi_ns('type'))

src/zeep/xsd/schema.py

+4
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ def merge(self, schema):
213213
self.documents.add(document)
214214
self._prefix_map_auto = self._create_prefix_map()
215215

216+
def deserialize(self, node):
217+
elm = self.get_element(node.tag)
218+
return elm.parse(node, schema=self)
219+
216220
def _load_default_documents(self):
217221
schema = SchemaDocument(ns.XSD, None, None)
218222

tests/test_xsd_any.py

+51
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,54 @@ def test_any_in_nested_sequence():
411411
assert item.items._value_1 == datetime.date(2016, 7, 4)
412412
assert item.version == 'str1234'
413413
assert item._value_1 == [datetime.date(2016, 7, 4), True]
414+
415+
416+
def test_element_any_parse_inline_schema():
417+
node = load_xml("""
418+
<xsd:schema
419+
elementFormDefault="qualified"
420+
targetNamespace="https://tests.python-zeep.org"
421+
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
422+
<xsd:element name="container">
423+
<xsd:complexType>
424+
<xsd:sequence>
425+
<xsd:element ref="xsd:schema"/>
426+
<xsd:any/>
427+
</xsd:sequence>
428+
</xsd:complexType>
429+
</xsd:element>
430+
</xsd:schema>
431+
""")
432+
433+
schema = xsd.Schema(node)
434+
435+
node = load_xml("""
436+
<OstatDepoResult>
437+
<xs:schema id="OD" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
438+
<xs:element name="OD" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
439+
<xs:complexType>
440+
<xs:choice minOccurs="0" maxOccurs="unbounded">
441+
<xs:element name="odr">
442+
<xs:complexType>
443+
<xs:sequence>
444+
<xs:element name="item" msdata:Caption="item" type="xs:string" minOccurs="0" />
445+
</xs:sequence>
446+
</xs:complexType>
447+
</xs:element>
448+
</xs:choice>
449+
</xs:complexType>
450+
</xs:element>
451+
</xs:schema>
452+
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
453+
<OD xmlns="">
454+
<odr diffgr:id="odr1" msdata:rowOrder="0">
455+
<item>hetwerkt</item>
456+
</odr>
457+
</OD>
458+
</diffgr:diffgram>
459+
</OstatDepoResult>
460+
""")
461+
462+
elm = schema.get_element('ns0:container')
463+
data = elm.parse(node, schema)
464+
assert data._value_1._value_1[0]['odr']['item'] == 'hetwerkt'

0 commit comments

Comments
 (0)