codeant-ai-for-open-source[bot] commented on code in PR #44549:
URL: https://github.com/apache/superset/pull/44549#discussion_r4078001986
##########
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:
**Suggestion:** Truthiness-only metadata validation lets legacy contexts
with malformed non-empty datasource or query values reach the factory, so reads
can still raise instead of returning `None`.
**Assessment:** ๐ `Major` ยท ๐ `Occurrence: Rarely` ยท ๐ท๏ธ `Error handling`
[](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=1c90314ecaf84186a387d3baeb00ba6e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=1c90314ecaf84186a387d3baeb00ba6e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
<details>
<summary><b>Prompt for AI Agent ๐ค </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/models/slice.py
**Line:** 418:424
**Comment:**
*Error Handling: Truthiness-only metadata validation lets legacy
contexts with malformed non-empty datasource or query values reach the factory,
so reads can still raise instead of returning `None`.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44549&comment_hash=ca87bc7a398823d8aeac25c99bb0d27b0b0baf56395e93d5a49aa134d75fa7be&reaction=like'>๐</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44549&comment_hash=ca87bc7a398823d8aeac25c99bb0d27b0b0baf56395e93d5a49aa134d75fa7be&reaction=dislike'>๐</a>
##########
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:
**Suggestion:** `datasource` and each query are only checked for truthiness,
so malformed values such as missing datasource `type` still pass validation and
later raise `KeyError` or `TypeError`.
**Assessment:** ๐ `Major` ยท ๐ `Occurrence: Rarely` ยท ๐ท๏ธ `Type error`
[](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=d1399fab3d5c44458d5b9dc95613ad71&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=d1399fab3d5c44458d5b9dc95613ad71&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
<details>
<summary><b>Prompt for AI Agent ๐ค </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/utils/schema.py
**Line:** 69:73
**Comment:**
*Type Error: `datasource` and each query are only checked for
truthiness, so malformed values such as missing datasource `type` still pass
validation and later raise `KeyError` or `TypeError`.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44549&comment_hash=c351c2f22d212f388e82287a7c919a3285a14b3564d80bcb96180a8711d95ca0&reaction=like'>๐</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44549&comment_hash=c351c2f22d212f388e82287a7c919a3285a14b3564d80bcb96180a8711d95ca0&reaction=dislike'>๐</a>
--
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]