mirror of https://github.com/apache/superset.git
fix(jinja): extract form_data from json body (#10684)
* fix(jinja): extract form_data from json body * add test * disable test for presto
This commit is contained in:
parent
dd0bc472e3
commit
58b075bc17
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue