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


##########
superset/commands/query/create.py:
##########
@@ -0,0 +1,75 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+from functools import partial
+from typing import Any
+
+from flask import g
+from flask_appbuilder.models.sqla import Model
+from marshmallow import ValidationError
+
+from superset.commands.base import BaseCommand, CreateMixin
+from superset.commands.query.exceptions import (
+    SavedQueryCreateFailedError,
+    SavedQueryInvalidError,
+)
+from superset.daos.query import SavedQueryDAO
+from superset.utils.decorators import on_error, transaction
+
+logger = logging.getLogger(__name__)
+
+
+class CreateSavedQueryCommand(CreateMixin, BaseCommand):
+    def __init__(self, data: dict[str, Any]):
+        self._properties = data.copy()
+
+    @transaction(on_error=partial(on_error, 
reraise=SavedQueryCreateFailedError))
+    def run(self) -> Model:
+        self.validate()
+        self._properties["user_id"] = g.user.id
+        saved_query = SavedQueryDAO.create(attributes=self._properties)
+        return saved_query
+
+    def validate(self) -> None:
+        from superset.extensions import db, security_manager
+        from superset.models.core import Database
+
+        exceptions: list[ValidationError] = []
+
+        db_id = self._properties.get("db_id")
+        if not db_id:
+            exceptions.append(ValidationError("db_id is required", 
field_name="db_id"))
+            raise SavedQueryInvalidError(exceptions=exceptions)

Review Comment:
   **Suggestion:** The `db_id` presence check uses a falsy test, which 
incorrectly treats valid integer-like values such as `0` as "missing" and 
returns the wrong validation error path. This should check explicitly for 
`None` (and optionally rely on type validation) so missing and invalid IDs are 
handled consistently with the update command. [incorrect condition logic]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ create_saved_query MCP tool misreports db_id validation failures.
   - ⚠️ Database-not-found checks in create command are bypassed for 0.
   - ⚠️ Inconsistent db_id handling versus update_saved_query command.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. From an MCP client, call the `create_saved_query` tool as documented in
   `superset/mcp_service/saved_query/tool/create_saved_query.py:43-45`, e.g. 
`await
   client.call_tool("create_saved_query", {"request": {"label": "test", "sql": 
"SELECT 1",
   "db_id": 0}})`.
   
   2. The request is validated by the Pydantic schema `CreateSavedQueryRequest` 
in
   `superset/mcp_service/saved_query/schemas.py:23-43`, where `db_id` is 
defined as a
   required `int` (line 38–43); `0` is accepted as a valid integer and passed 
through as
   `request.db_id`.
   
   3. In `create_saved_query`, properties are built with `"db_id": 
request.db_id` at
   `superset/mcp_service/saved_query/tool/create_saved_query.py:68-72`, and
   `CreateSavedQueryCommand(properties).run()` is invoked inside the event 
logger context at
   line 80–81.
   
   4. `CreateSavedQueryCommand.validate` in 
`superset/commands/query/create.py:47-56` loads
   `db_id = self._properties.get("db_id")` (line 53), then evaluates `if not 
db_id:` (line
   54); because `0` is falsy, it appends a `ValidationError("db_id is required",
   field_name="db_id")` (line 55) and raises `SavedQueryInvalidError` (line 
56). The tool's
   error handler at `create_saved_query.py:100-110` returns a 
`CreateSavedQueryResponse` with
   `error` set to this message, incorrectly reporting `db_id` as missing even 
though it was
   provided, and never running the database existence/access checks at 
`create.py:58-75`.
   ```
   </details>
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=6dc34974908846a1b25fde24faac72ca&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 | [Fix in VSCode 
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=6dc34974908846a1b25fde24faac72ca&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/commands/query/create.py
   **Line:** 53:56
   **Comment:**
        *Incorrect Condition Logic: The `db_id` presence check uses a falsy 
test, which incorrectly treats valid integer-like values such as `0` as 
"missing" and returns the wrong validation error path. This should check 
explicitly for `None` (and optionally rely on type validation) so missing and 
invalid IDs are handled consistently with the update command.
   
   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%2F40361&comment_hash=7c73892796c8db5f71c82280a02656abc49c717a4e7abdbe7241ca745c846a32&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40361&comment_hash=7c73892796c8db5f71c82280a02656abc49c717a4e7abdbe7241ca745c846a32&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