codeant-ai-for-open-source[bot] commented on code in PR #41300: URL: https://github.com/apache/superset/pull/41300#discussion_r3456305190
########## tests/unit_tests/config_swagger_ui_test.py: ########## @@ -0,0 +1,50 @@ +# 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. +"""Tests for environment-driven Swagger UI config defaults.""" + +import importlib + +import pytest + + [email protected]( + "env_value, expected", + [ + (None, False), # unset -> off by default + ("true", True), + ("True", True), + ("false", False), + ("", False), + ], +) +def test_fab_api_swagger_ui_is_env_driven_and_off_by_default( + monkeypatch, env_value, expected +): + if env_value is None: + monkeypatch.delenv("SUPERSET_ENABLE_SWAGGER_UI", raising=False) + else: + monkeypatch.setenv("SUPERSET_ENABLE_SWAGGER_UI", env_value) + + import superset.config as config + + importlib.reload(config) + try: + assert config.FAB_API_SWAGGER_UI is expected + finally: + # Restore the module to its environment-default state for other tests. + monkeypatch.delenv("SUPERSET_ENABLE_SWAGGER_UI", raising=False) + importlib.reload(config) Review Comment: **Suggestion:** The cleanup block always deletes `SUPERSET_ENABLE_SWAGGER_UI` and reloads `superset.config`, which can leave the module in a state that does not match the process environment when the env var was originally set before the test started. Because `monkeypatch` restores environment variables after the test function exits (not before this reload), subsequent tests can observe a reloaded config computed from the wrong env state. Restore the original env value before the final reload (or avoid this manual reload pattern) so module state and environment stay consistent. [logic error] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ Leaves config.FAB_API_SWAGGER_UI desynced from process environment. - ⚠️ Later tests may see Swagger UI disabled despite env. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. In a shell, set `SUPERSET_ENABLE_SWAGGER_UI=true` before running tests so the process environment contains this variable (this is the env var read in `superset/config.py:428-432` to compute `FAB_API_SWAGGER_UI`). 2. Run `pytest tests/unit_tests/config_swagger_ui_test.py::test_fab_api_swagger_ui_is_env_driven_and_off_by_default` so the parametrized test in `tests/unit_tests/config_swagger_ui_test.py:34-36` executes. 3. For the `"true"` / `"True"` cases, inside the test `monkeypatch.setenv()` or `monkeypatch.delenv()` is called at `tests/unit_tests/config_swagger_ui_test.py:37-40`, `superset.config` is imported at line 42 and reloaded at line 44, so `config.FAB_API_SWAGGER_UI` is initially computed from the patched env and the assertion at line 46 passes. 4. In the `finally` block at `tests/unit_tests/config_swagger_ui_test.py:47-50`, `monkeypatch.delenv("SUPERSET_ENABLE_SWAGGER_UI", raising=False)` deletes the variable from `os.environ` and `importlib.reload(config)` re-imports `superset.config` with the env var missing, so `config.FAB_API_SWAGGER_UI` is recomputed as `False` (per `superset/config.py:431-432`), but after the test finishes pytest's monkeypatch teardown restores the original `SUPERSET_ENABLE_SWAGGER_UI=true` in the environment while leaving the already-reloaded `superset.config` module untouched; any later test or code in this process that reads `superset.config.FAB_API_SWAGGER_UI` will now see `False` even though `os.environ["SUPERSET_ENABLE_SWAGGER_UI"]` is `"true"`, demonstrating the leaked, inconsistent module state caused by the cleanup reload with a forcibly deleted env var. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=02f784b77058465a98ca2786b7cc0263&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=02f784b77058465a98ca2786b7cc0263&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:** tests/unit_tests/config_swagger_ui_test.py **Line:** 47:50 **Comment:** *Logic Error: The cleanup block always deletes `SUPERSET_ENABLE_SWAGGER_UI` and reloads `superset.config`, which can leave the module in a state that does not match the process environment when the env var was originally set before the test started. Because `monkeypatch` restores environment variables after the test function exits (not before this reload), subsequent tests can observe a reloaded config computed from the wrong env state. Restore the original env value before the final reload (or avoid this manual reload pattern) so module state and environment stay consistent. 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%2F41300&comment_hash=37aeaaa15fd3015f9e953320b184ad569234b9b8b95b17664f8c6ea42deea25c&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41300&comment_hash=37aeaaa15fd3015f9e953320b184ad569234b9b8b95b17664f8c6ea42deea25c&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]
