You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ma...@apache.org on 2018/01/12 19:11:33 UTC

[incubator-superset] branch master updated: [bugfix] dealing with DBAPIs that return unserilizable types (#4200)

This is an automated email from the ASF dual-hosted git repository.

maximebeauchemin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 269f55c  [bugfix] dealing with DBAPIs that return unserilizable types (#4200)
269f55c is described below

commit 269f55c29ac318aa3a21d91e4af039c138bd8f2f
Author: Maxime Beauchemin <ma...@gmail.com>
AuthorDate: Fri Jan 12 11:11:31 2018 -0800

    [bugfix] dealing with DBAPIs that return unserilizable types (#4200)
    
    Funky datatypes in some databases like BLOBs will have the DBAPI return
    python types that can't be serialized to JSON out of the box.
    
    Currently, when this happens SQL Lab fails in a bad way with a gigantic
    HTML error message.
    
    This allows specifying a pessimistic JSON serializer handler that will
    simply show "Unserializable [type]"
---
 superset/utils.py      | 16 +++++++++++++---
 superset/views/core.py |  4 +++-
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/superset/utils.py b/superset/utils.py
index ee81af5..8224843 100644
--- a/superset/utils.py
+++ b/superset/utils.py
@@ -299,7 +299,7 @@ def base_json_conv(obj):
         return str(obj)
 
 
-def json_iso_dttm_ser(obj):
+def json_iso_dttm_ser(obj, pessimistic=False):
     """
     json serializer that deals with dates
 
@@ -317,11 +317,21 @@ def json_iso_dttm_ser(obj):
     elif isinstance(obj, time):
         obj = obj.isoformat()
     else:
-        raise TypeError(
-            'Unserializable object {} of type {}'.format(obj, type(obj)))
+        if pessimistic:
+            return 'Unserializable [{}]'.format(type(obj))
+        else:
+            raise TypeError(
+                'Unserializable object {} of type {}'.format(obj, type(obj)))
     return obj
 
 
+def pessimistic_json_iso_dttm_ser(obj):
+    """Proxy to call json_iso_dttm_ser in a pessimistic way
+
+    If one of object is not serializable to json, it will still succeed"""
+    return json_iso_dttm_ser(obj, pessimistic=True)
+
+
 def datetime_to_epoch(dttm):
     if dttm.tzinfo:
         epoch_with_tz = pytz.utc.localize(EPOCH)
diff --git a/superset/views/core.py b/superset/views/core.py
index 3e18cf3..1cd3593 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -2316,12 +2316,14 @@ class Superset(BaseSupersetView):
                 data = sql_lab.get_sql_results(
                     query_id=query_id, return_results=True,
                     template_params=template_params)
+            payload = json.dumps(
+                data, default=utils.pessimistic_json_iso_dttm_ser)
         except Exception as e:
             logging.exception(e)
             return json_error_response('{}'.format(e))
         if data.get('status') == QueryStatus.FAILED:
             return json_error_response(payload=data)
-        return json_success(json.dumps(data, default=utils.json_iso_dttm_ser))
+        return json_success(payload)
 
     @has_access
     @expose('/csv/<client_id>')

-- 
To stop receiving notification emails like this one, please contact
['"commits@superset.apache.org" <co...@superset.apache.org>'].