From 9b9a3d53a901da7ef2b80f62e7e6a668be161bec Mon Sep 17 00:00:00 2001 From: gengjh Date: Fri, 12 Apr 2013 20:44:41 +0800 Subject: Set empty element to "" When serializer xml formatter response need set empty element to "" to align with JSON formatter. Fix bug 1168317 Change-Id: I4985791095f43eac88fe42ec16f6a78bbf77ec07 --- keystone/common/serializer.py | 4 ++++ tests/test_content_types.py | 50 +++++++++++++++++++++++++++++++++++++++++++ tests/test_serializer.py | 23 ++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/keystone/common/serializer.py b/keystone/common/serializer.py index 71cca4a5..6b12df3a 100644 --- a/keystone/common/serializer.py +++ b/keystone/common/serializer.py @@ -153,6 +153,10 @@ class XmlDeserializer(object): else: values = dict(values.items() + child.items()) + # set empty and none-list element to None to align with JSON + if not values: + values = "" + d = {XmlDeserializer._tag_name(element.tag, namespace): values} if links: diff --git a/tests/test_content_types.py b/tests/test_content_types.py index a7000691..9e989c7a 100644 --- a/tests/test_content_types.py +++ b/tests/test_content_types.py @@ -904,3 +904,53 @@ class XmlTestCase(RestfulTestCase, CoreApiTests): """, expected_status=400) + + def test_add_tenant_xml(self): + """ + verify create a tenant without providing description field + """ + token = self.get_scoped_token() + r = self.request( + port=self._admin_port(), + method='POST', + path='/v2.0/tenants', + headers={ + 'Content-Type': 'application/xml', + 'X-Auth-Token': token + }, + body=""" + + + + + """) + self._from_content_type(r, 'json') + self.assertIsNotNone(r.body.get('tenant')) + self.assertValidTenant(r.body['tenant']) + self.assertEqual(r.body['tenant'].get('description'), "") + + def test_add_tenant_json(self): + """ + verify create a tenant without providing description field + """ + token = self.get_scoped_token() + r = self.request( + port=self._admin_port(), + method='POST', + path='/v2.0/tenants', + headers={ + 'Content-Type': 'application/json', + 'X-Auth-Token': token + }, + body=""" + {"tenant":{ + "name":"test1", + "description":"", + "enabled":"true"} + } + """) + self._from_content_type(r, 'json') + self.assertIsNotNone(r.body.get('tenant')) + self.assertValidTenant(r.body['tenant']) + self.assertEqual(r.body['tenant'].get('description'), "") diff --git a/tests/test_serializer.py b/tests/test_serializer.py index b440c815..ebeb3f3e 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -154,6 +154,29 @@ class XmlSerializerTestCase(test.TestCase): self.assertSerializeDeserialize(d, xml) + def test_tenant_crud_no_description(self): + d = { + "tenant": { + "id": "1234", + "name": "ACME corp", + "description": "", + "enabled": True + } + } + + xml = """ + + + + + """ + + self.assertSerializeDeserialize(d, xml) + def test_policy_list(self): d = {"policies": [{"id": "ab12cd"}]} -- cgit