Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor(routes): extract parse_bool_arg helper for query-string booleans (#3842)

LearningCircuitaee51e03d8ac
diff --git a/src/local_deep_research/research_library/routes/rag_routes.py b/src/local_deep_research/research_library/routes/rag_routes.py
index c6448aed3a..9a5ebeb3f0 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -35,6 +35,7 @@ from typing import Optional
from ...constants import FILE_PATH_SENTINELS, FILE_PATH_TEXT_ONLY
from ...security.decorators import require_json_body
from ...web.auth.decorators import login_required
+from ...web.utils.request_helpers import parse_bool_arg
from ...utilities.db_utils import get_settings_manager
from ..services.library_rag_service import LibraryRAGService
from ...settings.manager import SettingsManager
@@ -354,7 +355,7 @@ def test_embedding():
"""Test an embedding configuration by generating a test embedding."""
try:
- data = request.json
+ data = request.get_json()
provider = data.get("provider")
model = data.get("model")
test_text = data.get("test_text", "This is a test.")
@@ -677,7 +678,7 @@ def index_all():
from ...database.session_context import get_user_db_session
from ...database.library_init import get_default_library_id
- force_reindex = request.args.get("force_reindex", "false").lower() == "true"
+ force_reindex = parse_bool_arg("force_reindex")
username = session["username"]
# Get collection_id from request or use default Library collection
@@ -1026,7 +1027,7 @@ def index_local_library():
file_patterns = request.args.get(
"patterns", "*.pdf,*.txt,*.md,*.html"
).split(",")
- recursive = request.args.get("recursive", "true").lower() == "true"
+ recursive = parse_bool_arg("recursive", default=True)
if not folder_path:
return jsonify({"success": False, "error": "Path is required"}), 400
@@ -1810,7 +1811,7 @@ def index_collection(collection_id):
from ...database.session_context import get_user_db_session
from ...database.session_passwords import session_password_store
- force_reindex = request.args.get("force_reindex", "false").lower() == "true"
+ force_reindex = parse_bool_arg("force_reindex")
username = session["username"]
session_id = session.get("session_id")
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 619c45e32f..a3b2d6bec4 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -66,6 +66,7 @@ from ...utilities.db_utils import get_settings_manager
from ...utilities.url_utils import normalize_url
from ...security.decorators import require_json_body
from ..auth.decorators import login_required
+from ..utils.request_helpers import parse_bool_arg
from ...security.rate_limiter import settings_limit
from ...settings.manager import get_typed_setting_value, parse_boolean
from ..services.settings_service import (
@@ -1086,14 +1087,10 @@ def api_get_available_models():
"""Get available LLM models from various providers"""
endpoint_start = time.perf_counter()
try:
- from flask import request
-
from ...database.models import ProviderModel
# Check if force_refresh is requested
- force_refresh = (
- request.args.get("force_refresh", "false").lower() == "true"
- )
+ force_refresh = parse_bool_arg("force_refresh")
# Get all auto-discovered providers (show all so users can discover
# and configure providers they haven't set up yet)
diff --git a/src/local_deep_research/web/utils/request_helpers.py b/src/local_deep_research/web/utils/request_helpers.py
new file mode 100644
index 0000000000..569694cfc7
--- /dev/null
+++ b/src/local_deep_research/web/utils/request_helpers.py
@@ -0,0 +1,16 @@
+"""Helpers for reading values from the Flask request object."""
+
+from flask import request
+
+
+def parse_bool_arg(name: str, default: bool = False) -> bool:
+ """Parse a query-string parameter as a boolean.
+
+ Returns ``True`` only when the raw value lowercases to ``"true"``;
+ returns *default* when the parameter is absent. Whitespace is not
+ stripped, matching the inline pattern this helper replaces.
+ """
+ raw = request.args.get(name)
+ if raw is None:
+ return default
+ return raw.lower() == "true"
diff --git a/tests/web/utils/test_request_helpers.py b/tests/web/utils/test_request_helpers.py
new file mode 100644
index 0000000000..ca8ee8f3a3
--- /dev/null
+++ b/tests/web/utils/test_request_helpers.py
@@ -0,0 +1,98 @@
+"""Tests for web/utils/request_helpers.py.
+
+Locks in parse_bool_arg's behavior contract: it MUST behave identically to
+the inline pattern ``request.args.get(name, "false_or_true").lower() == "true"``
+that it replaces. In particular: no whitespace stripping, no widening of the
+truthy set beyond the literal string ``"true"`` (case-insensitive).
+"""
+
+from flask import Flask
+
+from local_deep_research.web.utils.request_helpers import parse_bool_arg
+
+
+def _make_app():
+ """Create a minimal Flask test app."""
+ app = Flask(__name__)
+ app.config["TESTING"] = True
+ return app
+
+
+class TestParseBoolArg:
+ """Tests for parse_bool_arg."""
+
+ def test_missing_param_returns_default_false(self):
+ """Absent param returns the default (False)."""
+ app = _make_app()
+ with app.test_request_context("/?other=value"):
+ assert parse_bool_arg("flag") is False
+
+ def test_missing_param_returns_default_true(self):
+ """Absent param returns the default (True) when default=True."""
+ app = _make_app()
+ with app.test_request_context("/?other=value"):
+ assert parse_bool_arg("flag", default=True) is True
+
+ def test_value_true_lowercase(self):
+ """?flag=true returns True."""
+ app = _make_app()
+ with app.test_request_context("/?flag=true"):
+ assert parse_bool_arg("flag") is True
+
+ def test_value_true_uppercase(self):
+ """?flag=TRUE returns True (case-insensitive)."""
+ app = _make_app()
+ with app.test_request_context("/?flag=TRUE"):
+ assert parse_bool_arg("flag") is True
+
+ def test_value_true_mixed_case(self):
+ """?flag=True returns True (case-insensitive)."""
+ app = _make_app()
+ with app.test_request_context("/?flag=True"):
+ assert parse_bool_arg("flag") is True
+
+ def test_value_false(self):
+ """?flag=false returns False."""
+ app = _make_app()
+ with app.test_request_context("/?flag=false"):
+ assert parse_bool_arg("flag", default=True) is False
+
+ def test_empty_value_returns_false(self):
+ """?flag= (empty string) returns False, NOT the default.
+
+ This matches the inline pattern: ``request.args.get("flag", "false")``
+ returns the empty string when the param is present-but-empty, not the
+ default. The empty string lowercases to "" which != "true", so False.
+ """
+ app = _make_app()
+ with app.test_request_context("/?flag="):
+ # Even with default=True, an empty value still returns False.
+ assert parse_bool_arg("flag", default=True) is False
+
+ def test_value_yes_returns_false(self):
+ """?flag=yes returns False — only "true" is truthy."""
+ app = _make_app()
+ with app.test_request_context("/?flag=yes"):
+ assert parse_bool_arg("flag") is False
+
+ def test_value_one_returns_false(self):
+ """?flag=1 returns False — only "true" is truthy."""
+ app = _make_app()
+ with app.test_request_context("/?flag=1"):
+ assert parse_bool_arg("flag") is False
+
+ def test_value_on_returns_false(self):
+ """?flag=on returns False — only "true" is truthy."""
+ app = _make_app()
+ with app.test_request_context("/?flag=on"):
+ assert parse_bool_arg("flag") is False
+
+ def test_whitespace_padded_true_returns_false(self):
+ """?flag=%20true%20 returns False — whitespace is NOT stripped.
+
+ This locks in the no-strip behavior: a stripping helper would return
+ True here, which would diverge from the original inline pattern.
+ """
+ app = _make_app()
+ with app.test_request_context("/?flag=%20true%20"):
+ assert parse_bool_arg("flag") is False