1. Christophe de Vienne
  2. tgws

Commits

Christophe de Vienne  committed 74867cc

Fix xml null values encoding/decoding

  • Participants
  • Parent commits 348370d
  • Branches default

Comments (0)

Files changed (3)

File tgwebservices/iconv.py

View file
  • Ignore whitespace
     if isinstance(itype, list):
         itemtype = itype[0]
         items = []
-    pass
-
-def _get_single_value(elem, itype):
-    """Converts a single XML element into the given type"""
-    if isinstance(itype, list):
-        itemtype = itype[0]
-        items = []
         for subelem in elem.getchildren():
             items.append(_get_single_value(subelem, itemtype))
         return items
         itype = itype.type
     if not isinstance(itype, type):
         itype = type(itype)
+    if elem.get('nil') == 'true':
+        return None
     if itype not in primitives:
         return _xml_to_instance(elem, itype)
     if itype == bool:

File tgwebservices/tests/test_xml.py

View file
  • Ignore whitespace
 
     assert data == rdata[::-1]
 
+def test_null():
+    cherrypy.root = ComplexService("http://foo.bar.baz/")
+    
+    request = """<request><rwp><value nil="true"/></rwp></request>"""
+    testutil.create_request("/getandsetrwprop", rfile=StringIO.StringIO(request), 
+                            method="POST", 
+                            headers={"Content-Length" : str(len(request)),
+                                     "Content-Type" : 
+                                        "text/xml; charset=utf-8",
+                                     "Accept" : 
+                                        "text/xml"})
+    output = cherrypy.response.body[0]
+    print output
+    assert output == """<result><value nil="true"/></result>"""
+

File tgwebservices/xml_.py

View file
  • Ignore whitespace
     elem(value.base64())
     return elem
 
-@when(xml_value, "type(value) not in primitives")
+@when(xml_value, "value is None")
+def xml_none(name, value):
+    elem = Element(name, nil='true')
+    return elem
+
+@when(xml_value, "value is not None and type(value) not in primitives")
 def xml_instance(name, value):
     """Handles an instance of a complex type."""
     elem = Element(name)