You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by vi...@apache.org on 2020/08/28 18:26:33 UTC

[incubator-superset] branch master updated: fix(jinja): extract form_data from json body (#10684)

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

villebro 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 58b075b  fix(jinja): extract form_data from json body (#10684)
58b075b is described below

commit 58b075bc1756268292b31887dc76122e9d651026
Author: Ville Brofeldt <33...@users.noreply.github.com>
AuthorDate: Fri Aug 28 21:26:07 2020 +0300

    fix(jinja): extract form_data from json body (#10684)
    
    * fix(jinja): extract form_data from json body
    
    * add test
    
    * disable test for presto
---
 superset/views/utils.py   |  8 ++++++++
 tests/charts/api_tests.py | 22 +++++++++++++++++++++-
 tests/datasource_tests.py |  2 --
 3 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/superset/views/utils.py b/superset/views/utils.py
index 2a8b2cc..7e50800 100644
--- a/superset/views/utils.py
+++ b/superset/views/utils.py
@@ -116,8 +116,16 @@ def get_form_data(
     slice_id: Optional[int] = None, use_slice_data: bool = False
 ) -> Tuple[Dict[str, Any], Optional[Slice]]:
     form_data = {}
+    # chart data API requests are JSON
+    request_json_data = (
+        request.json["queries"][0]
+        if request.is_json and "queries" in request.json
+        else None
+    )
     request_form_data = request.form.get("form_data")
     request_args_data = request.args.get("form_data")
+    if request_json_data:
+        form_data.update(request_json_data)
     if request_form_data:
         form_data.update(json.loads(request_form_data))
     # request params can overwrite the body
diff --git a/tests/charts/api_tests.py b/tests/charts/api_tests.py
index 48e929d..75485e3 100644
--- a/tests/charts/api_tests.py
+++ b/tests/charts/api_tests.py
@@ -774,7 +774,7 @@ class TestChartApi(SupersetTestCase, ApiOwnersTestCaseMixin):
         self.login(username="admin")
         table = self.get_table_by_name("birth_names")
         request_payload = get_query_context(table.name, table.id, table.type)
-        request_payload["result_type"] = "query"
+        request_payload["result_type"] = utils.ChartDataResultType.QUERY
         rv = self.post_assert_metric(CHART_DATA_URI, request_payload, "data")
         self.assertEqual(rv.status_code, 200)
 
@@ -901,3 +901,23 @@ class TestChartApi(SupersetTestCase, ApiOwnersTestCaseMixin):
         payload = get_query_context(table.name, table.id, table.type)
         rv = self.post_assert_metric(CHART_DATA_URI, payload, "data")
         self.assertEqual(rv.status_code, 401)
+
+    def test_chart_data_jinja_filter_request(self):
+        """
+        Chart data API: Ensure request referencing filters via jinja renders a correct query
+        """
+        self.login(username="admin")
+        table = self.get_table_by_name("birth_names")
+        request_payload = get_query_context(table.name, table.id, table.type)
+        request_payload["result_type"] = utils.ChartDataResultType.QUERY
+        request_payload["queries"][0]["filters"] = [
+            {"col": "gender", "op": "==", "val": "boy"}
+        ]
+        request_payload["queries"][0]["extras"][
+            "where"
+        ] = "('boy' = '{{ filter_values('gender', 'xyz' )[0] }}')"
+        rv = self.post_assert_metric(CHART_DATA_URI, request_payload, "data")
+        response_payload = json.loads(rv.data.decode("utf-8"))
+        result = response_payload["result"][0]["query"]
+        if get_example_database().backend != "presto":
+            assert "('boy' = 'boy')" in result
diff --git a/tests/datasource_tests.py b/tests/datasource_tests.py
index b0bae9d..5fd81c0 100644
--- a/tests/datasource_tests.py
+++ b/tests/datasource_tests.py
@@ -18,8 +18,6 @@
 import json
 from copy import deepcopy
 
-from superset.utils.core import get_or_create_db
-
 from .base_tests import SupersetTestCase
 from .fixtures.datasource import datasource_post