[json] enable jsonexport selection on empty rsets (closes #2544358) stable
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 20 Dec 2012 12:15:57 +0100
branchstable
changeset 8629 3ae893f9ec84
parent 8628 b236b91836c0
child 8630 b81231749b16
[json] enable jsonexport selection on empty rsets (closes #2544358)
web/test/unittest_views_json.py
web/views/json.py
--- a/web/test/unittest_views_json.py	Thu Dec 20 12:14:14 2012 +0100
+++ b/web/test/unittest_views_json.py	Thu Dec 20 12:15:57 2012 +0100
@@ -37,6 +37,13 @@
         self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
         self.assertEqual(data, '[["guests", 1], ["managers", 1]]')
 
+    def test_json_rsetexport_empty_rset(self):
+        req = self.request()
+        rset = req.execute('Any X WHERE X is CWUser, X login "foobarbaz"')
+        data = self.view('jsonexport', rset)
+        self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/json'])
+        self.assertEqual(data, '[]')
+
     def test_json_rsetexport_with_jsonp(self):
         req = self.request()
         req.form.update({'callback': 'foo',
--- a/web/views/json.py	Thu Dec 20 12:14:14 2012 +0100
+++ b/web/views/json.py	Thu Dec 20 12:15:57 2012 +0100
@@ -23,6 +23,7 @@
 _ = unicode
 
 from cubicweb.utils import json_dumps
+from cubicweb.predicates import any_rset
 from cubicweb.view import EntityView, AnyRsetView
 from cubicweb.web.application import anonymized_request
 from cubicweb.web.views import basecontrollers
@@ -90,6 +91,7 @@
 class JsonRsetView(JsonMixIn, AnyRsetView):
     """dumps raw result set in JSON format"""
     __regid__ = 'jsonexport'
+    __select__ = any_rset() # means rset might be empty or have any shape
     title = _('json-export-view')
 
     def call(self):