rusackas commented on code in PR #44549:
URL: https://github.com/apache/superset/pull/44549#discussion_r4080745626
##########
superset/models/slice.py:
##########
@@ -404,12 +405,26 @@ def form_data(self) -> dict[str, Any]:
def get_query_context(self) -> QueryContext | None:
if self.query_context:
try:
- return self.get_query_context_factory().create(
- **{**json.loads(self.query_context), "current_slice": self}
- )
+ query_context_payload = json.loads(self.query_context)
except json.JSONDecodeError as ex:
logger.error("Malformed json in slice's query context",
exc_info=True)
logger.exception(ex)
+ return None
+ # ``QueryContextFactory.create()`` requires ``datasource`` and
+ # ``queries`` as keyword-only arguments; a row saved before the
+ # ChartPostSchema/ChartPutSchema validation added in
+ # apache/superset#35774 can still be missing either, which would
+ # otherwise raise a raw TypeError on every read of this chart.
+ if not is_query_context_metadata_complete(query_context_payload):
+ logger.error(
+ "Slice %s query_context is missing required "
+ "'datasource'/'queries' fields",
+ self.id,
+ )
+ return None
Review Comment:
Good catch, tightened `is_query_context_metadata_complete` to require
`datasource` carry `id`/`type` instead of just being truthy, since that's what
`_convert_to_model` actually indexes.
##########
superset/utils/schema.py:
##########
@@ -56,6 +56,44 @@ def validate_json(value: Union[bytes, bytearray, str]) ->
None:
raise ValidationError("JSON not valid") from ex
+def is_query_context_metadata_complete(value: Any) -> bool:
+ """
+ Whether a parsed ``query_context`` payload carries the ``datasource`` and
+ ``queries`` keys ``QueryContextFactory.create()`` requires (apache/superset
+ #35774): both are required keyword-only arguments there, and a chart saved
+ without them fails every subsequent read with a raw ``TypeError`` instead
+ of a clear error at save time.
+
+ :param value: the object obtained by JSON-decoding a query_context string
+ """
+ return (
+ isinstance(value, dict)
+ and bool(value.get("datasource"))
+ and bool(value.get("queries"))
+ )
Review Comment:
Fixed the datasource half, now requires `id`/`type` instead of a truthy
check. Left per-query validation alone, query object shape varies too much by
viz type to check without risking false rejections on legit payloads.
##########
superset/models/slice.py:
##########
@@ -449,7 +464,7 @@ def slice_link(self) -> Markup:
# SCRIPT_NAME (the application_root). `Slice.url` itself stays router-
# relative so frontend callers can apply ensureAppRoot exactly once.
href = url_for("ExploreView.root", slice_id=self.id)
- return Markup(f'<a href="{href}">{name}</a>')
+ return Markup('<a href="{}">{}</a>').format(href, name)
Review Comment:
That's the current code already, two `{}` placeholders and two args (`href`,
`name`). Might be reviewing a stale diff.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]