bito-code-review[bot] commented on code in PR #44549:
URL: https://github.com/apache/superset/pull/44549#discussion_r4082354504


##########
superset/utils/schema.py:
##########
@@ -56,6 +56,54 @@ 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. ``datasource`` must be a mapping carrying
+    the ``id``/``type`` keys ``DatasourceDict`` requires -- 
``_convert_to_model``
+    indexes both directly and raises ``KeyError`` if either is absent -- but
+    ``queries`` may legitimately be an empty list (a chart with no query
+    objects yet still round-trips through ``create()`` without error), so only
+    its presence as a list is checked, not its truthiness.
+
+    :param value: the object obtained by JSON-decoding a query_context string
+    """
+    if not isinstance(value, dict) or not isinstance(value.get("queries"), 
list):
+        return False
+    datasource = value.get("datasource")
+    return (
+        isinstance(datasource, dict)
+        and bool(datasource.get("id"))
+        and bool(datasource.get("type"))

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>invalid datasource type unvalidated</b></div>
   <div id="fix">
   
   `bool(datasource.get("type"))` only checks presence, but 
`QueryContextFactory._convert_to_model` calls 
`DatasourceType(datasource["type"])`, which raises `ValueError` for any 
non-member string. A chart saved with an invalid `type` passes this check yet 
still raises a raw `ValueError` on every read via `Slice.get_query_context` — 
the same failure class the docstring says this guard prevents. Consider 
validating the value against `DatasourceType` members.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #df4558</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
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]

Reply via email to