You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2026/08/25 15:13:09 UTC
(superset) branch fix-chartdata-invalid-date-range-500 created (now a45f1ddbf59)
This is an automated email from the ASF dual-hosted git repository.
eschutho pushed a change to branch fix-chartdata-invalid-date-range-500
in repository https://gitbox.apache.org/repos/asf/superset.git
at a45f1ddbf59 fix(charts/data): return 400 instead of 500 on reversed date range (SC-118279)
This branch includes the following new commits:
new a45f1ddbf59 fix(charts/data): return 400 instead of 500 on reversed date range (SC-118279)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(superset) 01/01: fix(charts/data): return 400 instead of 500 on reversed date range (SC-118279)
Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
eschutho pushed a commit to branch fix-chartdata-invalid-date-range-500
in repository https://gitbox.apache.org/repos/asf/superset.git
commit a45f1ddbf594342c0a0ff156ca39d10e11fd1480
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Tue Aug 25 15:10:24 2026 +0000
fix(charts/data): return 400 instead of 500 on reversed date range (SC-118279)
When a user submits a chart data request with a reversed custom date range
(since > until), get_since_until() in superset/utils/date_parser.py raises a
bare ValueError("From date cannot be larger than to date"). This propagates
through ChartDataQueryContextSchema().load() (via its make_query_context
@post_load hook) and out of _create_query_context_from_form(), which only
caught KeyError. The callers catch marshmallow.ValidationError but not
ValueError, so it surfaced as an unhandled 500.
Add a ValueError arm next to the existing KeyError arm in
_create_query_context_from_form(), re-raising as marshmallow.ValidationError so
the API returns a 400. The raised type of get_since_until() is deliberately
left unchanged, since other call sites explicitly catch ValueError today.
Fixes SUPERSET-PYTHON-NA4
Co-Authored-By: Claude <no...@anthropic.com>
---
superset/charts/data/api.py | 2 ++
tests/integration_tests/charts/data/api_tests.py | 9 +++++++++
tests/unit_tests/charts/test_chart_data_api.py | 20 ++++++++++++++++++++
3 files changed, 31 insertions(+)
diff --git a/superset/charts/data/api.py b/superset/charts/data/api.py
index fc0feaf31df..2dabb537d98 100644
--- a/superset/charts/data/api.py
+++ b/superset/charts/data/api.py
@@ -729,6 +729,8 @@ class ChartDataRestApi(ChartRestApi):
return ChartDataQueryContextSchema().load(form_data)
except KeyError as ex:
raise ValidationError("Request is incorrect") from ex
+ except ValueError as ex:
+ raise ValidationError(str(ex)) from ex
def _should_use_streaming(
self, result: dict[Any, Any], form_data: dict[str, Any] | None = None
diff --git a/tests/integration_tests/charts/data/api_tests.py b/tests/integration_tests/charts/data/api_tests.py
index 18a30c543fd..4a54f7ffa54 100644
--- a/tests/integration_tests/charts/data/api_tests.py
+++ b/tests/integration_tests/charts/data/api_tests.py
@@ -363,6 +363,15 @@ class TestPostChartDataApi(BaseTestChartDataApi):
rv = self.post_assert_metric(CHART_DATA_URI, self.query_context_payload, "data")
assert rv.status_code == 400
+ def test_with_reversed_time_range__400(self):
+ # A reversed custom date range (since > until) makes get_since_until
+ # raise a ValueError; the API must convert it to a 400, not a 500.
+ self.query_context_payload["queries"][0]["time_range"] = (
+ "2024-01-01T00:00:00 : 2020-01-01T00:00:00"
+ )
+ 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_invalid_payload__400(self):
invalid_query_context = {"form_data": "NOT VALID JSON"}
diff --git a/tests/unit_tests/charts/test_chart_data_api.py b/tests/unit_tests/charts/test_chart_data_api.py
index 1c88bceb647..950b103892c 100644
--- a/tests/unit_tests/charts/test_chart_data_api.py
+++ b/tests/unit_tests/charts/test_chart_data_api.py
@@ -858,3 +858,23 @@ def test_send_chart_response_does_not_double_extension_for_csv_filename() -> Non
content_disposition = response.headers["Content-Disposition"]
assert "my_export.csv.csv" not in content_disposition
assert "my_export.csv" in content_disposition
+
+
+def test_create_query_context_from_form_converts_value_error_to_400() -> None:
+ """
+ A ValueError raised while loading the query context (e.g. a reversed date
+ range where since > until) is re-raised as a marshmallow ValidationError so
+ the API returns a 400 instead of an unhandled 500.
+ """
+ from marshmallow import ValidationError
+
+ api = ChartDataRestApi()
+ message = "From date cannot be larger than to date"
+ with patch(
+ "superset.charts.data.api.ChartDataQueryContextSchema.load",
+ side_effect=ValueError(message),
+ ):
+ with pytest.raises(ValidationError) as excinfo:
+ api._create_query_context_from_form({})
+
+ assert message in str(excinfo.value)