Skip to content

Commit fe057be

Browse files
Merge pull request #604 from maykinmedia/xsi-nil-on-complex-type-with-simple-content
Deal with Nil on a complexType with simpleContent
2 parents 8166b8b + d88d427 commit fe057be

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/zeep/xsd/types/simple.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from lxml import etree
55

66
from zeep.exceptions import ValidationError
7-
from zeep.xsd.const import xsd_ns
7+
from zeep.xsd.const import Nil, xsd_ns, xsi_ns
88
from zeep.xsd.types.any import AnyType
99

1010
logger = logging.getLogger(__name__)
@@ -68,6 +68,9 @@ def pythonvalue(self, xmlvalue):
6868
'%s.pytonvalue() not implemented' % self.__class__.__name__)
6969

7070
def render(self, parent, value, xsd_type=None, render_path=None):
71+
if value is Nil:
72+
parent.set(xsi_ns('nil'), 'true')
73+
return
7174
parent.text = self.xmlvalue(value)
7275

7376
def signature(self, schema=None, standalone=True):

tests/test_xsd_complex_types.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,38 @@ def test_xml_unparsed_elements():
295295
obj = container_elm.parse(expected[0], schema)
296296
assert obj.item == 'bar'
297297
assert obj._raw_elements
298+
299+
300+
def test_xml_simple_content_nil():
301+
schema = xsd.Schema(load_xml("""
302+
<?xml version="1.0"?>
303+
<schema xmlns="http://www.w3.org/2001/XMLSchema"
304+
xmlns:tns="http://tests.python-zeep.org/"
305+
targetNamespace="http://tests.python-zeep.org/"
306+
elementFormDefault="qualified">
307+
<element name="container" nillable="true">
308+
<complexType>
309+
<simpleContent>
310+
<restriction base="string">
311+
<maxLength value="1000"/>
312+
</restriction>
313+
</simpleContent>
314+
</complexType>
315+
</element>
316+
</schema>
317+
"""))
318+
schema.set_ns_prefix('tns', 'http://tests.python-zeep.org/')
319+
container_elm = schema.get_element('tns:container')
320+
obj = container_elm(xsd.Nil)
321+
result = render_node(container_elm, obj)
322+
323+
expected = """
324+
<document>
325+
<ns0:container xmlns:ns0="http://tests.python-zeep.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
326+
</document>
327+
"""
328+
result = render_node(container_elm, obj)
329+
assert_nodes_equal(result, expected)
330+
331+
obj = container_elm.parse(result[0], schema)
332+
assert obj._value_1 is None

0 commit comments

Comments
 (0)