Skip to content

Commit

Permalink
check for empty request
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro committed Feb 8, 2022
1 parent ee93f12 commit 9246e81
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
27 changes: 15 additions & 12 deletions superset/charts/data/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,22 +348,25 @@ def _send_chart_response(
if not security_manager.can_access("can_csv", "Superset"):
return self.response_403()

if not result["queries"]:
return self.response_400(_("Empty query result"))

if len(result["queries"]) == 1:
# return single query results csv format
data = result["queries"][0]["data"]
return CsvResponse(data, headers=generate_download_headers("csv"))
else:
# return multi-query csv results bundled as a zip file
encoding = current_app.config["CSV_EXPORT"].get("encoding", "utf-8")
files = {
f"query_{idx + 1}.csv": result["data"].encode(encoding)
for idx, result in enumerate(result["queries"])
}
return Response(
create_zip(files),
headers=generate_download_headers("zip"),
mimetype="application/zip",
)

# return multi-query csv results bundled as a zip file
encoding = current_app.config["CSV_EXPORT"].get("encoding", "utf-8")
files = {
f"query_{idx + 1}.csv": result["data"].encode(encoding)
for idx, result in enumerate(result["queries"])
}
return Response(
create_zip(files),
headers=generate_download_headers("zip"),
mimetype="application/zip",
)

if result_format == ChartDataResultFormat.JSON:
response_data = simplejson.dumps(
Expand Down
10 changes: 10 additions & 0 deletions tests/integration_tests/charts/data/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ def test_with_query_result_type__200(self):
rv = self.post_assert_metric(CHART_DATA_URI, self.query_context_payload, "data")
assert rv.status_code == 200

@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
def test_empty_request_with_csv_result_format(self):
"""
Chart data API: Test empty chart data with CSV result format
"""
self.query_context_payload["result_format"] = "csv"
self.query_context_payload["queries"] = []
rv = self.post_assert_metric(CHART_DATA_URI, self.query_context_payload, "data")
assert rv.status_code == 400

@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
def test_with_csv_result_format(self):
"""
Expand Down

0 comments on commit 9246e81

Please sign in to comment.