mirror of
https://github.com/apache/superset.git
synced 2026-06-10 10:09:14 +00:00
Compare commits
1 Commits
feat/post-
...
fix/jinja-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
58246f0fd4 |
@@ -22,15 +22,7 @@ from typing import Any, TYPE_CHECKING
|
||||
|
||||
from flask import current_app
|
||||
from flask_babel import gettext as _
|
||||
from marshmallow import (
|
||||
EXCLUDE,
|
||||
fields,
|
||||
post_load,
|
||||
Schema,
|
||||
validate,
|
||||
validates_schema,
|
||||
ValidationError,
|
||||
)
|
||||
from marshmallow import EXCLUDE, fields, post_load, Schema, validate
|
||||
from marshmallow.validate import Length, Range
|
||||
from marshmallow_union import Union
|
||||
|
||||
@@ -945,42 +937,6 @@ class ChartDataPostProcessingOperationSchema(Schema):
|
||||
},
|
||||
)
|
||||
|
||||
# Map post-processing operation -> its options schema, for operations that
|
||||
# declare one. Operations without a dedicated schema are not structurally
|
||||
# validated here.
|
||||
_OPTIONS_SCHEMAS: dict[str, type[Schema]] = {
|
||||
"aggregate": ChartDataAggregateOptionsSchema,
|
||||
"rolling": ChartDataRollingOptionsSchema,
|
||||
"select": ChartDataSelectOptionsSchema,
|
||||
"sort": ChartDataSortOptionsSchema,
|
||||
"contribution": ChartDataContributionOptionsSchema,
|
||||
"prophet": ChartDataProphetOptionsSchema,
|
||||
"boxplot": ChartDataBoxplotOptionsSchema,
|
||||
"pivot": ChartDataPivotOptionsSchema,
|
||||
"geohash_decode": ChartDataGeohashDecodeOptionsSchema,
|
||||
"geohash_encode": ChartDataGeohashEncodeOptionsSchema,
|
||||
"geodetic_parse": ChartDataGeodeticParseOptionsSchema,
|
||||
}
|
||||
|
||||
@validates_schema
|
||||
def validate_options(self, data: dict[str, Any], **kwargs: Any) -> None:
|
||||
"""Validate ``options`` against the operation's option schema.
|
||||
|
||||
Validation is lenient (unknown keys are ignored) so it surfaces wrong
|
||||
types / out-of-range values on declared fields without rejecting
|
||||
payloads that carry extra keys.
|
||||
"""
|
||||
operation = data.get("operation")
|
||||
options = data.get("options")
|
||||
if not isinstance(operation, str) or not isinstance(options, dict):
|
||||
return
|
||||
schema_cls = self._OPTIONS_SCHEMAS.get(operation)
|
||||
if schema_cls is None:
|
||||
return
|
||||
errors = schema_cls(unknown=EXCLUDE).validate(options)
|
||||
if errors:
|
||||
raise ValidationError({"options": errors})
|
||||
|
||||
|
||||
class ChartDataFilterSchema(Schema):
|
||||
col = fields.Raw(
|
||||
|
||||
@@ -288,11 +288,16 @@ class ExtraCache:
|
||||
from superset.views.utils import get_form_data
|
||||
|
||||
if has_request_context() and request.args.get(param):
|
||||
return request.args.get(param, default)
|
||||
result = request.args.get(param, default)
|
||||
else:
|
||||
form_data, _ = get_form_data()
|
||||
url_params = form_data.get("url_params") or {}
|
||||
result = url_params.get(param, default)
|
||||
|
||||
form_data, _ = get_form_data()
|
||||
url_params = form_data.get("url_params") or {}
|
||||
result = url_params.get(param, default)
|
||||
# Apply the same handling to every input source. Values read from
|
||||
# request.args must go through the dialect-specific quoting below just
|
||||
# like values sourced from form_data, so the result is consistent
|
||||
# regardless of where the parameter originated.
|
||||
if result and escape_result and self.dialect:
|
||||
# use the dialect specific quoting logic to escape string
|
||||
result = String().literal_processor(dialect=self.dialect)(value=result)[
|
||||
|
||||
@@ -152,53 +152,3 @@ def test_time_grain_validation_with_config_addons(app_context: None) -> None:
|
||||
}
|
||||
result = schema.load(custom_data)
|
||||
assert result["time_grain"] == "PT10M"
|
||||
|
||||
|
||||
def test_post_processing_operation_validates_options(app_context: None) -> None:
|
||||
"""options are validated against the operation's option schema (leniently)."""
|
||||
from superset.charts.schemas import ChartDataPostProcessingOperationSchema
|
||||
|
||||
schema = ChartDataPostProcessingOperationSchema()
|
||||
|
||||
# Valid prophet options load.
|
||||
schema.load(
|
||||
{
|
||||
"operation": "prophet",
|
||||
"options": {
|
||||
"time_grain": "P1D",
|
||||
"periods": 7,
|
||||
"confidence_interval": 0.8,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
# Out-of-range confidence_interval (must be 0-1) on a declared field is
|
||||
# rejected.
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load(
|
||||
{
|
||||
"operation": "prophet",
|
||||
"options": {
|
||||
"time_grain": "P1D",
|
||||
"periods": 7,
|
||||
"confidence_interval": 2.0,
|
||||
},
|
||||
}
|
||||
)
|
||||
assert "options" in exc_info.value.messages
|
||||
|
||||
# Extra/unknown keys are tolerated (lenient validation).
|
||||
schema.load(
|
||||
{
|
||||
"operation": "prophet",
|
||||
"options": {
|
||||
"time_grain": "P1D",
|
||||
"periods": 7,
|
||||
"confidence_interval": 0.8,
|
||||
"some_future_option": True,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
# An operation without a dedicated schema accepts arbitrary options.
|
||||
schema.load({"operation": "flatten", "options": {"anything": [1, 2, 3]}})
|
||||
|
||||
@@ -438,6 +438,26 @@ def test_url_param_unescaped_default_form_data() -> None:
|
||||
assert cache.url_param("bar", "O'Malley", escape_result=False) == "O'Malley"
|
||||
|
||||
|
||||
def test_url_param_escaped_query() -> None:
|
||||
"""
|
||||
Test that a ``url_param`` value read from the request query string is
|
||||
handled the same way as one sourced from ``form_data`` -- i.e. it goes
|
||||
through the dialect-specific quoting instead of being returned raw.
|
||||
"""
|
||||
with current_app.test_request_context(query_string={"foo": "O'Brien"}):
|
||||
cache = ExtraCache(dialect=dialect())
|
||||
assert cache.url_param("foo") == "O''Brien"
|
||||
|
||||
|
||||
def test_url_param_unescaped_query() -> None:
|
||||
"""
|
||||
Test that ``escape_result=False`` returns the raw query-string value.
|
||||
"""
|
||||
with current_app.test_request_context(query_string={"foo": "O'Brien"}):
|
||||
cache = ExtraCache(dialect=dialect())
|
||||
assert cache.url_param("foo", escape_result=False) == "O'Brien"
|
||||
|
||||
|
||||
def test_safe_proxy_primitive() -> None:
|
||||
"""
|
||||
Test the ``safe_proxy`` helper with a function returning a ``str``.
|
||||
|
||||
Reference in New Issue
Block a user