codeant-ai-for-open-source[bot] commented on code in PR #42474:
URL: https://github.com/apache/superset/pull/42474#discussion_r3656606712


##########
superset/connectors/sqla/models.py:
##########
@@ -169,6 +167,37 @@ class MetadataResult:
 ]
 
 
+def _get_query_context_column_names(slc: Slice) -> set[str] | None:
+    """Extract column names from a chart's serialized query context."""
+    if not slc.query_context:
+        return None
+
+    try:
+        query_context = json.loads(slc.query_context)
+    except json.JSONDecodeError as ex:
+        logger.error("Malformed json in slice's query context", exc_info=True)
+        logger.exception(ex)
+        return None
+
+    if not isinstance(query_context, dict):
+        return None
+
+    queries = query_context.get("queries")
+    if not isinstance(queries, list):
+        return None
+
+    column_names: set[str] = set()
+    for query in queries:
+        if not isinstance(query, dict):
+            continue
+        columns = query["columns"] if "columns" in query else 
query.get("groupby")
+        if not isinstance(columns, list):
+            continue
+        column_names.update(utils.get_column_name(column_) for column_ in 
columns)

Review Comment:
   **Suggestion:** Only the container type is validated before passing each 
item to `utils.get_column_name`. Malformed column entries such as `None` or 
dictionaries missing the expected column fields can raise an exception, 
aborting dashboard serialization instead of being treated as malformed query 
context and falling back to form data. Validate or safely handle each column 
entry before updating the set. [possible bug]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Dashboard serialization can fail for malformed chart query context.
   - ⚠️ The affected slice prevents reliable column metadata generation.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Load a dashboard slice through `BaseDatasource.data_for_slices()` in
   `superset/connectors/sqla/models.py` with a persisted serialized 
`query_context` whose
   `queries` entry contains a `columns` list with an invalid item such as 
`None` or a
   dictionary lacking the fields expected by `utils.get_column_name()`.
   
   2. `_get_query_context_column_names()` validates only that `columns` is a 
list at
   `superset/connectors/sqla/models.py:193-195`; it does not validate 
individual entries.
   
   3. At `superset/connectors/sqla/models.py:196`, the generator passes each 
malformed item
   directly to `utils.get_column_name()`. If that utility dereferences the 
expected column
   structure, it raises while the generator is being evaluated.
   
   4. The exception is not caught by `_get_query_context_column_names()` 
because its
   exception handling covers only JSON decoding at lines 175-180, so 
`data_for_slices()`
   fails instead of using its form-data fallback at line 561.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=44f4781df2444b1a8e5a0fe39108cfda&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=44f4781df2444b1a8e5a0fe39108cfda&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/connectors/sqla/models.py
   **Line:** 196:196
   **Comment:**
        *Possible Bug: Only the container type is validated before passing each 
item to `utils.get_column_name`. Malformed column entries such as `None` or 
dictionaries missing the expected column fields can raise an exception, 
aborting dashboard serialization instead of being treated as malformed query 
context and falling back to form data. Validate or safely handle each column 
entry before updating the set.
   
   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%2F42474&comment_hash=3b2500ab6bf1627940b3745ecd7b6e6b03cb11afcc4b0f6f042c72cb4f3e87e7&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42474&comment_hash=3b2500ab6bf1627940b3745ecd7b6e6b03cb11afcc4b0f6f042c72cb4f3e87e7&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]

Reply via email to