This is an automated email from the ASF dual-hosted git repository.

villebro pushed a commit to branch 7.0
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 67b0fd39e6a80115a5f25a6f68822471e9023007
Author: Amin Ghadersohi <[email protected]>
AuthorDate: Thu Nov 12 09:46:15 2026 +0000

    fix(api): return 200 from the database and tag _info endpoints without a 
filter (#46058)
    
    (cherry picked from commit 58a1a5c7e8f759383573ba15ba509e8d1bc49015)
---
 superset/views/base_api.py                         |  29 +++++
 tests/unit_tests/views/test_info_without_filter.py | 132 +++++++++++++++++++++
 2 files changed, 161 insertions(+)

diff --git a/superset/views/base_api.py b/superset/views/base_api.py
index 8c4398725e7..19e49d1f489 100644
--- a/superset/views/base_api.py
+++ b/superset/views/base_api.py
@@ -517,6 +517,35 @@ class BaseSupersetModelRestApi(BaseSupersetApiMixin, 
ModelRestApi):
             self.add_columns = [model_id]
         super()._init_properties()
 
+    def _get_fields_info(
+        self,
+        cols: list[str],
+        model_schema: Schema,
+        filter_rel_fields: Any,
+        **kwargs: Any,
+    ) -> list[dict[str, Any]]:
+        """
+        Describe the add/edit fields exposed by the ``_info`` endpoint.
+
+        Overrides :meth:`flask_appbuilder.api.ModelRestApi._get_fields_info`, 
which
+        indexes the marshmallow schema with every column in ``add_columns`` /
+        ``edit_columns``. APIs that declare an explicit add/edit schema can 
list
+        columns the schema does not define (for example a legacy alias handled 
by a
+        ``pre_load`` hook), and the lookup then fails the whole ``_info`` 
request with
+        a 500 whenever it is called without a ``keys`` filter. Columns without 
a
+        schema field cannot be described, so they are skipped.
+
+        :param cols: columns to describe
+        :param model_schema: marshmallow schema used to build the field details
+        :param filter_rel_fields: ``add_query_rel_fields`` or 
``edit_query_rel_fields``
+        :param kwargs: rison arguments for related-field pagination
+        :returns: field details for the columns defined by the schema
+        """
+        schema_cols = [col for col in cols if col in model_schema.fields]
+        return super()._get_fields_info(
+            schema_cols, model_schema, filter_rel_fields, **kwargs
+        )
+
     def _handle_filters_args(self, rison_args: dict[str, Any]) -> Filters:
         """
         Build a request-scoped ``Filters`` instance from Rison-encoded args.
diff --git a/tests/unit_tests/views/test_info_without_filter.py 
b/tests/unit_tests/views/test_info_without_filter.py
new file mode 100644
index 00000000000..0c863e57cc2
--- /dev/null
+++ b/tests/unit_tests/views/test_info_without_filter.py
@@ -0,0 +1,132 @@
+# 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.
+from typing import Any
+
+import pytest
+from flask import current_app
+from flask_appbuilder import ModelRestApi
+
+
[email protected]("resource", ["database", "tag"])
+def test_info_without_filter_returns_metadata(
+    client: Any,
+    full_api_access: None,
+    resource: str,
+) -> None:
+    """
+    ``GET /api/v1/<resource>/_info`` without a ``q`` filter builds the 
add/edit field
+    metadata for every declared column. Columns missing from the explicit 
add/edit
+    schemas must be skipped instead of raising a 500.
+    """
+    response = client.get(f"/api/v1/{resource}/_info")
+
+    assert response.status_code == 200
+    payload = response.json
+    assert "permissions" in payload
+    assert "add_columns" in payload
+    assert "edit_columns" in payload
+
+
+def test_database_info_describes_only_schema_fields(
+    client: Any,
+    full_api_access: None,
+) -> None:
+    """
+    The database add/edit schemas expose ``masked_encrypted_extra``, not the 
legacy
+    ``encrypted_extra`` column, so only the schema-backed fields are described.
+    """
+    response = client.get("/api/v1/database/_info")
+
+    assert response.status_code == 200
+    for key in ("add_columns", "edit_columns"):
+        names = {field["name"] for field in response.json[key]}
+        assert "database_name" in names
+        assert "encrypted_extra" not in names
+
+
+def test_every_model_api_info_without_filter_succeeds(
+    client: Any,
+    full_api_access: None,
+) -> None:
+    """
+    Guard all ``ModelRestApi`` views that expose ``_info`` against a 500 when 
the
+    endpoint is called without a filter.
+    """
+    failures = {}
+    for view in current_app.appbuilder.baseviews:
+        if not isinstance(view, ModelRestApi):
+            continue
+        if view.include_route_methods is not None and (
+            "info" not in view.include_route_methods
+        ):
+            continue
+        if "info" in (view.exclude_route_methods or set()):
+            continue
+        response = client.get(f"{view.route_base}/_info")
+        if response.status_code != 200:
+            failures[type(view).__name__] = response.status_code
+
+    assert failures == {}
+
+
+# Columns listed in ``add_columns`` / ``edit_columns`` that the explicit 
add/edit
+# schemas intentionally don't define, so ``_info`` skips them.
+EXPECTED_COLUMNS_WITHOUT_SCHEMA_FIELD = {
+    "DatabaseRestApi": {"encrypted_extra"},
+    "TagRestApi": {
+        "changed_by",
+        "changed_on",
+        "created_by",
+        "created_on",
+        "objects",
+        "type",
+        "users_favorited",
+    },
+}
+
+
+def test_info_skips_only_expected_columns(
+    client: Any,
+    full_api_access: None,
+) -> None:
+    """
+    ``_info`` silently skips columns missing from the add/edit schema, so guard
+    against new mismatches (e.g. a typo in ``add_columns``) going unnoticed.
+    """
+    skipped = {}
+    for view in current_app.appbuilder.baseviews:
+        if not isinstance(view, ModelRestApi):
+            continue
+        if view.include_route_methods is not None and (
+            "info" not in view.include_route_methods
+        ):
+            continue
+        if "info" in (view.exclude_route_methods or set()):
+            continue
+        missing = {
+            col
+            for cols, schema in (
+                (view.add_columns, view.add_model_schema),
+                (view.edit_columns, view.edit_model_schema),
+            )
+            for col in cols
+            if col not in schema.fields
+        }
+        if missing:
+            skipped[type(view).__name__] = missing
+
+    assert skipped == EXPECTED_COLUMNS_WITHOUT_SCHEMA_FIELD

Reply via email to