Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

chore: bump restx. now deco has type (#37391)

Asuka Minato64a99c5556e5
diff --git a/api/controllers/common/errors.py b/api/controllers/common/errors.py
index a8fdaf9810b..d720f892f41 100644
--- a/api/controllers/common/errors.py
+++ b/api/controllers/common/errors.py
@@ -170,7 +170,7 @@ class InvalidArgumentError(BaseHTTPException):
def register_permission_error_handler(api: ExternalApi) -> None:
"""Preserve the default permission-error response for Console and Service API."""
- @api.errorhandler(NoPermissionError)
+ @api.errorhandler(NoPermissionError) # pyrefly: ignore[not-callable] - Flask-RESTX errorhandler overload
def handle_permission_error(error: NoPermissionError) -> tuple[dict[str, str | int], int]:
got_request_exception.send(current_app, exception=error)
return {
@@ -185,9 +185,16 @@ def register_permission_error_handler(api: ExternalApi) -> None:
def register_credential_refresh_error_handler(api: ExternalApi) -> None:
- """Give temporary refresh failures the same retryable response on each API surface."""
+ """Give temporary refresh failures the same retryable response on each API surface.
- @api.errorhandler(CredentialRefreshUnavailableError)
+ Preconditions: The API supports exception-specific error registration.
+ Postconditions: Refresh failures return HTTP 503 before the generic handler.
+ Existing exception handlers and public error documentation remain registered.
+ """
+ register_handler = api.errorhandler(CredentialRefreshUnavailableError)
+ assert callable(register_handler)
+
+ @register_handler
def handle_refresh_unavailable(error: CredentialRefreshUnavailableError) -> tuple[dict[str, str | int], int]:
got_request_exception.send(current_app, exception=error)
status = HTTPStatus.SERVICE_UNAVAILABLE
diff --git a/api/controllers/console/__init__.py b/api/controllers/console/__init__.py
index 96253ec6c6f..c72913a7101 100644
--- a/api/controllers/console/__init__.py
+++ b/api/controllers/console/__init__.py
@@ -54,7 +54,7 @@ api.document_generic_errors(
)
[email protected](ActiveWorkspaceRequiredError)
[email protected](ActiveWorkspaceRequiredError) # pyrefly: ignore[not-callable] - Flask-RESTX decorator typing
def _handle_active_workspace_required_error(error: ActiveWorkspaceRequiredError):
"""Map a broken Console admission invariant without exposing internal details."""
got_request_exception.send(current_app, exception=error)
@@ -66,7 +66,7 @@ def _handle_active_workspace_required_error(error: ActiveWorkspaceRequiredError)
}, status.value
[email protected](WorkflowRunArchiveInvalidMonthError)
[email protected](WorkflowRunArchiveInvalidMonthError) # pyrefly: ignore[not-callable] - Flask-RESTX errorhandler overload
def _handle_workflow_run_archive_invalid_month_error(
error: WorkflowRunArchiveInvalidMonthError,
) -> tuple[dict[str, str | int], int]:
@@ -75,8 +75,8 @@ def _handle_workflow_run_archive_invalid_month_error(
return {"code": "bad_request", "message": str(error), "status": status.value}, status.value
[email protected](WorkflowRunArchiveNotFoundError)
[email protected](WorkflowRunArchiveDownloadTaskNotFoundError)
[email protected](WorkflowRunArchiveNotFoundError) # pyrefly: ignore[not-callable] - Flask-RESTX errorhandler overload
[email protected](WorkflowRunArchiveDownloadTaskNotFoundError) # pyrefly: ignore[not-callable] - Flask-RESTX errorhandler overload
def _handle_workflow_run_archive_not_found_error(
error: WorkflowRunArchiveNotFoundError | WorkflowRunArchiveDownloadTaskNotFoundError,
) -> tuple[dict[str, str | int], int]:
@@ -85,7 +85,7 @@ def _handle_workflow_run_archive_not_found_error(
return {"code": "not_found", "message": str(error), "status": status.value}, status.value
[email protected](WorkflowRunArchiveDownloadNotReadyError)
[email protected](WorkflowRunArchiveDownloadNotReadyError) # pyrefly: ignore[not-callable] - Flask-RESTX errorhandler overload
def _handle_workflow_run_archive_download_not_ready_error(
error: WorkflowRunArchiveDownloadNotReadyError,
) -> tuple[dict[str, str | int], int]:
diff --git a/api/controllers/inner_api/mail.py b/api/controllers/inner_api/mail.py
index 4ee4a0e5602..6107adc77a0 100644
--- a/api/controllers/inner_api/mail.py
+++ b/api/controllers/inner_api/mail.py
@@ -1,4 +1,4 @@
-from typing import Any
+from typing import Any, override
from flask_restx import Resource
from pydantic import BaseModel, Field
@@ -50,6 +50,7 @@ class EnterpriseMail(BaseMail):
@inner_api_ns.response(200, "Email sent successfully", inner_api_ns.models[SimpleMessageResponse.__name__])
@inner_api_only
@setup_required
+ @override
def post(self):
"""Send internal email for enterprise features.
@@ -72,6 +73,7 @@ class BillingMail(BaseMail):
)
@inner_api_only
@setup_required
+ @override
def post(self):
"""Send internal email for billing notifications.
diff --git a/api/libs/external_api.py b/api/libs/external_api.py
index 72bf7a7c3e1..6bafcb28b55 100644
--- a/api/libs/external_api.py
+++ b/api/libs/external_api.py
@@ -570,11 +570,11 @@ def register_external_error_handlers(api: Api, body_formatter: ErrorBodyFormatte
return _finalize(e, data, status_code), status_code
- api.errorhandler(HTTPException)(handle_http_exception)
- api.errorhandler(ValueError)(handle_value_error)
- api.errorhandler(AppInvokeQuotaExceededError)(handle_quota_exceeded)
- api.errorhandler(PluginRuntimeError)(handle_plugin_runtime_error)
- api.errorhandler(Exception)(handle_general_exception)
+ api.errorhandler(HTTPException)(handle_http_exception) # pyrefly: ignore[not-callable] - Flask-RESTX typing
+ api.errorhandler(ValueError)(handle_value_error) # pyrefly: ignore[not-callable] - Flask-RESTX typing
+ api.errorhandler(AppInvokeQuotaExceededError)(handle_quota_exceeded) # pyrefly: ignore[not-callable] - Flask-RESTX typing
+ api.errorhandler(PluginRuntimeError)(handle_plugin_runtime_error) # pyrefly: ignore[not-callable] - Flask-RESTX typing
+ api.errorhandler(Exception)(handle_general_exception) # pyrefly: ignore[not-callable] - Flask-RESTX typing
class ExternalApi(Api):
diff --git a/api/pyproject.toml b/api/pyproject.toml
index e226e804b67..78d2baf0ecb 100644
--- a/api/pyproject.toml
+++ b/api/pyproject.toml
@@ -72,7 +72,7 @@ exclude = ["providers/vdb/__pycache__", "providers/trace/__pycache__"]
[tool.uv.sources]
dify-agent = { path = "../dify-agent", editable = true }
-flask-restx = { git = "https://github.com/asukaminato0721/flask-restx", rev = "27758e26f8f740d7525d5039c51a9e524b6e2b68" }
+flask-restx = { git = "https://github.com/asukaminato0721/flask-restx", rev = "e44a2861b854" }
# shortcut: pin the 0.8.0 container-type fix until a release includes graphon#344.
graphon = { git = "https://github.com/langgenius/graphon", rev = "4fa22141774dff80e90e4dfa98ea3e6fbf0421bb" }
dify-vdb-alibabacloud-mysql = { workspace = true }
diff --git a/api/tests/e2e_tests/containers/controllers/console/workspace/test_members.py b/api/tests/e2e_tests/containers/controllers/console/workspace/test_members.py
index 2172a0b2149..ed3874d4066 100644
--- a/api/tests/e2e_tests/containers/controllers/console/workspace/test_members.py
+++ b/api/tests/e2e_tests/containers/controllers/console/workspace/test_members.py
@@ -27,7 +27,7 @@ StatusResponse = tuple[JsonResponse, int]
def unwrap(func: Callable[..., object]) -> Callable[..., object]:
- return cast(Callable[..., object], inspect.unwrap(func))
+ return inspect.unwrap(func)
def unwrap_status_response(func: Callable[..., object]) -> Callable[..., StatusResponse]:
diff --git a/api/tests/e2e_tests/containers/controllers/openapi/test_files.py b/api/tests/e2e_tests/containers/controllers/openapi/test_files.py
index d32ab95873e..f373f69f097 100644
--- a/api/tests/e2e_tests/containers/controllers/openapi/test_files.py
+++ b/api/tests/e2e_tests/containers/controllers/openapi/test_files.py
@@ -47,7 +47,7 @@ class TestAppFileUpload:
file=FileStorage(stream=BytesIO(content), filename="note.txt", content_type="text/plain")
)
with app.test_request_context(f"/openapi/v1/apps/{app_model.id}/files", method="POST"):
- result = api.post.__handler__(
+ result = vars(api.post)["__handler__"](
api,
context_for(
account,
diff --git a/api/tests/unit_tests/controllers/console/app/test_agent_app_access.py b/api/tests/unit_tests/controllers/console/app/test_agent_app_access.py
index a0b2c944af6..0c3fc5e3d4c 100644
--- a/api/tests/unit_tests/controllers/console/app/test_agent_app_access.py
+++ b/api/tests/unit_tests/controllers/console/app/test_agent_app_access.py
@@ -81,7 +81,9 @@ def test_features_parse_full_state_and_drop_soul_fields(app: Flask, services: Se
with app.test_request_context(
"/", method="POST", json={"opening_statement": "Hi", "speech_to_text": None, "model": {"name": "x"}}
):
- result = unwrap(agent_app_feature.AgentAppFeatureConfigResource.post)(object(), CONTEXT, AGENT_ID)
+ result = unwrap(agent_app_feature.AgentAppFeatureConfigResource.post)(
+ agent_app_feature.AgentAppFeatureConfigResource(), CONTEXT, AGENT_ID
+ )
assert result == {"result": "success"}
assert services.agent_apps.features.calls == [(CONTEXT, str(AGENT_ID), {"opening_statement": "Hi"})]
@@ -96,16 +98,17 @@ def test_feature_admission_enforces_editing_roles(
monkeypatch.setattr(flask_admission, "get_request_id", lambda: "request")
monkeypatch.setattr(flask_admission, "get_trace_id", lambda: None)
# Exercise the declared admission policy after setup/login/initialization.
- view = agent_app_feature.AgentAppFeatureConfigResource.post
- while hasattr(view.__wrapped__, "__wrapped__"):
- view = view.__wrapped__
+ view = unwrap(
+ agent_app_feature.AgentAppFeatureConfigResource.post,
+ stop=lambda handler: handler.__code__.co_name == "inject_request_context",
+ )
with app.test_request_context("/", method="POST", json={}):
if role in (TenantAccountRole.OWNER, TenantAccountRole.ADMIN, TenantAccountRole.EDITOR):
- assert view(object(), agent_id=AGENT_ID) == {"result": "success"}
+ assert view(agent_app_feature.AgentAppFeatureConfigResource(), agent_id=AGENT_ID) == {"result": "success"}
assert services.agent_apps.features.calls == [(CONTEXT, str(AGENT_ID), {})]
else:
with pytest.raises(Forbidden):
- view(object(), agent_id=AGENT_ID)
+ view(agent_app_feature.AgentAppFeatureConfigResource(), agent_id=AGENT_ID)
assert services.agent_apps.features.calls == []
diff --git a/api/tests/unit_tests/controllers/console/auth/test_data_source_oauth.py b/api/tests/unit_tests/controllers/console/auth/test_data_source_oauth.py
index 1864f2191a1..41d7368d83d 100644
--- a/api/tests/unit_tests/controllers/console/auth/test_data_source_oauth.py
+++ b/api/tests/unit_tests/controllers/console/auth/test_data_source_oauth.py
@@ -3,7 +3,7 @@ from inspect import unwrap
from unittest.mock import MagicMock, create_autospec, patch
from uuid import UUID
-from flask import Flask
+from flask import Flask, Response
from controllers.console.auth.data_source_oauth import (
OAuthDataSource,
@@ -81,6 +81,7 @@ def test_callback_parses_query_and_returns_flask_redirect() -> None:
):
response = OAuthDataSourceCallback().get("notion")
+ assert isinstance(response, Response)
assert response.status_code == HTTPStatus.FOUND
assert response.location == "https://console.example/root?lang=en&type=notion&code=code-1#top"
service.complete_callback.assert_called_once_with(code="code-1", error=None)
diff --git a/api/tests/unit_tests/controllers/console/auth/test_login.py b/api/tests/unit_tests/controllers/console/auth/test_login.py
index 69d04180dbc..9b3bc897f72 100644
--- a/api/tests/unit_tests/controllers/console/auth/test_login.py
+++ b/api/tests/unit_tests/controllers/console/auth/test_login.py
@@ -9,7 +9,7 @@ from types import SimpleNamespace
from typing import Never
import pytest
-from flask import Flask
+from flask import Flask, Response
from controllers.console import wraps
from controllers.console.auth import login
@@ -46,6 +46,7 @@ from services.entities.account_login_entities import (
PasswordLoginCommand,
PasswordLoginResult,
)
+from tests.helpers.unit.controllers.knowledge_fs_controller_fakes import narrowed
TEST_TOKEN = "00000000-0000-4000-8000-000000000001"
TOKENS = AccountSessionTokens(access_token="access-token", refresh_token="refresh-token", csrf_token="csrf-token")
@@ -133,7 +134,7 @@ def test_password_login_parses_input_and_serializes_cookies(
"invite_token": "invite-token",
},
):
- response = login.LoginApi().post()
+ response = narrowed(login.LoginApi().post(), Response)
assert response.json == {"result": "success", "data": None}
assert service.password_commands == [
@@ -160,7 +161,7 @@ def test_password_login_serializes_missing_workspace(
method="POST",
json={"email": "[email protected]", "password": _encode("password")},
):
- response = login.LoginApi().post()
+ response = narrowed(login.LoginApi().post(), dict)
assert response["result"] == "fail"
assert "workspace not found" in response["data"]
@@ -315,7 +316,7 @@ def test_refresh_maps_invalid_session_to_unauthorized(
monkeypatch.setattr(login, "extract_refresh_token", lambda _request: "bad-token")
with app.test_request_context("/refresh-token", method="POST"):
- response, status = login.RefreshTokenApi().post()
+ response, status = narrowed(login.RefreshTokenApi().post(), tuple)
assert status == 401
assert response == {"result": "fail", "message": "Invalid refresh token"}
@@ -329,7 +330,7 @@ def test_refresh_rejects_missing_cookie(
monkeypatch.setattr(login, "extract_refresh_token", lambda _request: None)
with app.test_request_context("/refresh-token", method="POST"):
- response, status = login.RefreshTokenApi().post()
+ response, status = narrowed(login.RefreshTokenApi().post(), tuple)
assert status == 401
assert response["message"] == "No refresh token provided"
@@ -347,7 +348,7 @@ def test_refresh_does_not_require_completed_setup(
monkeypatch.setattr(login, "extract_refresh_token", lambda _request: "old-refresh")
with app.test_request_context("/refresh-token", method="POST"):
- response = login.RefreshTokenApi().post()
+ response = narrowed(login.RefreshTokenApi().post(), Response)
assert response.json == {"result": "success"}
assert service.refresh_tokens == ["old-refresh"]
diff --git a/api/tests/unit_tests/controllers/console/auth/test_oauth.py b/api/tests/unit_tests/controllers/console/auth/test_oauth.py
index 38472867522..b4c81d2d927 100644
--- a/api/tests/unit_tests/controllers/console/auth/test_oauth.py
+++ b/api/tests/unit_tests/controllers/console/auth/test_oauth.py
@@ -6,6 +6,7 @@ from typing import NoReturn
import pytest
from flask import Flask
from werkzeug.exceptions import Forbidden, UnprocessableEntity
+from werkzeug.wrappers import Response
from controllers.console import wraps as console_wraps
from controllers.console.auth import oauth as oauth_controller
@@ -28,6 +29,7 @@ from services.entities.account_oauth_entities import (
OAuthSignInResult,
)
from services.system_feature_service import SystemFeatureService
+from tests.helpers.unit.controllers.knowledge_fs_controller_fakes import narrowed
CONSOLE_WEB_URL = "https://console.example.com"
@@ -83,7 +85,7 @@ def test_login_parses_input_and_delegates_to_application_service(
with app.test_request_context(
"/oauth/login/github?invite_token=invite&timezone=Asia%2FShanghai&language=zh-Hans&redirect_url=%2Fapps"
):
- response = OAuthLogin().get("github")
+ response = narrowed(OAuthLogin().get("github"), Response)
assert response.status_code == 302
assert response.headers["Location"] == "https://provider.example/authorize"
@@ -108,7 +110,7 @@ def test_login_returns_adapter_error_for_unknown_provider(
_install_service(monkeypatch, service)
with app.test_request_context("/oauth/login/unknown"):
- payload, status = OAuthLogin().get("unknown")
+ payload, status = narrowed(OAuthLogin().get("unknown"), tuple)
assert status == 400
assert payload == {"error": "Invalid provider"}
@@ -130,7 +132,7 @@ def test_oauth_admission_does_not_query_enterprise_features(
monkeypatch.setattr(SystemFeatureService, "get_license", unexpected_feature_query)
with app.test_request_context("/oauth/login/github"):
- response = OAuthLogin().get("github")
+ response = narrowed(OAuthLogin().get("github"), Response)
assert response.status_code == 302
assert service.authorization_calls
@@ -170,7 +172,7 @@ def test_callback_passes_stable_values_and_serializes_session_cookies(
f"/oauth/authorize/github?code=code-1&state={state}",
headers={"Accept-Language": "en-US,en;q=0.9"},
):
- response = OAuthCallback().get("github")
+ response = narrowed(OAuthCallback().get("github"), Response)
assert response.status_code == 302
assert response.headers["Location"] == "/apps?oauth_new_user=false"
@@ -213,7 +215,7 @@ def test_callback_serializes_safe_redirect_target(
state = encode_oauth_state(redirect_url=redirect_url)
with app.test_request_context(f"/oauth/authorize/github?code=code-1&state={state}"):
- response = OAuthCallback().get("github")
+ response = narrowed(OAuthCallback().get("github"), Response)
assert response.headers["Location"] == expected
@@ -227,7 +229,7 @@ def test_callback_serializes_invitation_completion_target(
_install_service(monkeypatch, service)
with app.test_request_context("/oauth/authorize/github?code=code-1"):
- response = OAuthCallback().get("github")
+ response = narrowed(OAuthCallback().get("github"), Response)
assert response.headers["Location"] == f"{CONSOLE_WEB_URL}/signin/invite-settings?invite_token=invite+token"
@@ -255,7 +257,7 @@ def test_callback_maps_oauth_processing_error_to_bad_request(
_install_service(monkeypatch, service)
with app.test_request_context("/oauth/authorize/github?code=code-1"):
- payload, status = OAuthCallback().get("github")
+ payload, status = narrowed(OAuthCallback().get("github"), tuple)
assert status == 400
assert payload == {"error": "OAuth process failed"}
@@ -282,7 +284,7 @@ def test_callback_serializes_application_errors_as_signin_redirects(
_install_service(monkeypatch, service)
with app.test_request_context("/oauth/authorize/github?code=code-1"):
- response = OAuthCallback().get("github")
+ response = narrowed(OAuthCallback().get("github"), Response)
assert response.status_code == 302
assert response.headers["Location"] == f"{CONSOLE_WEB_URL}/signin?{expected_query}"
diff --git a/api/tests/unit_tests/controllers/console/knowledge_fs/test_resources.py b/api/tests/unit_tests/controllers/console/knowledge_fs/test_resources.py
index 90b6306aa13..cdc08c4c7fe 100644
--- a/api/tests/unit_tests/controllers/console/knowledge_fs/test_resources.py
+++ b/api/tests/unit_tests/controllers/console/knowledge_fs/test_resources.py
@@ -84,8 +84,11 @@ from services.knowledge_fs.product_dto import (
KnowledgeFSNamespacePreviewPageResponse,
KnowledgeFSOverviewBaseStatsResponse,
KnowledgeFSOverviewQueryOutcomesResponse,
+ KnowledgeFSOverviewStatsResponse,
KnowledgeFSPublicFailureResponse,
+ KnowledgeFSQueryAdmissionResponse,
KnowledgeFSQueryCreatePayload,
+ KnowledgeFSQueryStreamCapabilityResponse,
KnowledgeFSSmallFileUploadResponse,
KnowledgeFSSourceImportConfigurationPayload,
KnowledgeFSSourceResponse,
@@ -96,6 +99,7 @@ from services.knowledge_fs.product_dto import (
KnowledgeFSSpaceCreateResponse,
KnowledgeFSStagedUploadResponse,
KnowledgeFSStreamCapabilityPayload,
+ KnowledgeFSStreamCapabilityResponse,
)
from services.knowledge_fs.product_remote import (
KnowledgeFSOperationUnavailableError,
@@ -177,7 +181,7 @@ class TestKnowledgeFsCreationCases:
with app.test_request_context("/knowledge-fs/spaces", method="POST", json={"name": "Test", "slug": "test"}):
if allowed:
- result, status = resources.KnowledgeFSSpacesApi().post()
+ result, status = narrowed(resources.KnowledgeFSSpacesApi().post(), tuple)
assert status == HTTPStatus.ACCEPTED
assert result["control_space_id"] == "space"
else:
@@ -1040,7 +1044,7 @@ class TestKnowledgeFsProductControllersCases:
{"$ref": "#/components/schemas/KnowledgeFSOnlineDriveWorkflowImportPayload"},
]
- api_doc = console_resources.KnowledgeFSSpaceSourceWorkflowImportApi.post.__apidoc__
+ api_doc = vars(console_resources.KnowledgeFSSpaceSourceWorkflowImportApi.post)["__apidoc__"]
assert api_doc["params"]["Idempotency-Key"]["required"] is True
assert api_doc["params"]["Idempotency-Key"]["minLength"] == 8
assert api_doc["params"]["Idempotency-Key"]["maxLength"] == 255
@@ -1428,8 +1432,8 @@ class TestKnowledgeFsProductControllersCases:
@staticmethod
def test_space_update_and_delete_publish_their_actual_http_status_contracts() -> None:
- patch_responses = console_resources.KnowledgeFSSpaceApi.patch.__apidoc__["responses"]
- delete_responses = console_resources.KnowledgeFSSpaceApi.delete.__apidoc__["responses"]
+ patch_responses = vars(console_resources.KnowledgeFSSpaceApi.patch)["__apidoc__"]["responses"]
+ delete_responses = vars(console_resources.KnowledgeFSSpaceApi.delete)["__apidoc__"]["responses"]
assert set(patch_responses) == {"200"}
assert set(delete_responses) == {"204"}
@@ -1737,7 +1741,7 @@ class TestKnowledgeFsProductControllersCases:
}
]
assert "token" not in response["url"].lower()
- assert console_resources.KnowledgeFSSpaceQueryStreamCapabilityApi.post.__apidoc__["deprecated"] is True
+ assert vars(console_resources.KnowledgeFSSpaceQueryStreamCapabilityApi.post)["__apidoc__"]["deprecated"] is True
@staticmethod
def test_query_admission_binds_validated_mode_to_resolved_kfs_space(monkeypatch: pytest.MonkeyPatch) -> None:
@@ -3509,6 +3513,7 @@ class TestKnowledgeFsResourceDelegationCases:
control_space_id="control-space-1",
)
]
+ result = narrowed(result, KnowledgeFSOverviewStatsResponse)
assert result.documents == 13
assert result.linked_apps == 7
assert result.queries.value == 120
@@ -3699,6 +3704,9 @@ class TestKnowledgeFsResourceDelegationCases:
"space-1",
)
+ query = narrowed(query, KnowledgeFSQueryAdmissionResponse)
+ task = narrowed(task, KnowledgeFSStreamCapabilityResponse)
+ legacy_query = narrowed(legacy_query, KnowledgeFSQueryStreamCapabilityResponse)
assert query.request.knowledge_space_id == "knowledge-space-1"
assert query.url == "https://dify.example/console/api/knowledge-fs/query-stream"
assert (
@@ -3736,6 +3744,7 @@ class TestKnowledgeFsResourceDelegationCases:
)
assert broker.issue_service.calls == [invocation(profile=profile, operation_id="createQuery")]
+ response = narrowed(response, KnowledgeFSQueryAdmissionResponse)
assert response.request.knowledge_space_id == "knowledge-space-1"
assert response.request.query == "question"
assert response.url == "https://api.dify.example/v1/knowledge-fs/query-stream"
diff --git a/api/tests/unit_tests/controllers/console/test_notification.py b/api/tests/unit_tests/controllers/console/test_notification.py
index 1b8a57f5e36..bb1b291c611 100644
--- a/api/tests/unit_tests/controllers/console/test_notification.py
+++ b/api/tests/unit_tests/controllers/console/test_notification.py
@@ -16,6 +16,7 @@ from libs.login import AccountWithTenant
from machinery.context import RequestContext
from services.entities.notification_entities import NotificationItem, NotificationResult
from tests.helpers.unit.config_override import apply_config_overrides
+from tests.helpers.unit.controllers.knowledge_fs_controller_fakes import narrowed
from tests.helpers.unit.model_factories import make_account
@@ -38,6 +39,10 @@ def test_get_notification_validates_language_query_and_serializes_result(
query_string: dict[str, str],
expected_language: str,
) -> None:
+ """Preconditions: The account is admitted and the service returns a notification.
+
+ Postconditions: The handler returns HTTP 200, serializes the notification, and preserves the request context.
+ """
service = Mock()
service.get_active.return_value = NotificationResult(
should_show=True,
@@ -67,7 +72,7 @@ def test_get_notification_validates_language_query_and_serializes_result(
app.test_request_context("/notification", query_string=query_string),
patch("controllers.console.notification.application_services", return_value=services),
):
- result, status = api.get()
+ result, status = narrowed(api.get(), tuple)
assert status == 200
assert result == {
diff --git a/api/tests/unit_tests/controllers/console/test_spec.py b/api/tests/unit_tests/controllers/console/test_spec.py
index 87a032b64ca..74aa5698d13 100644
--- a/api/tests/unit_tests/controllers/console/test_spec.py
+++ b/api/tests/unit_tests/controllers/console/test_spec.py
@@ -2,11 +2,14 @@ from inspect import unwrap
from types import SimpleNamespace
from unittest.mock import create_autospec, patch
+from flask import Response
+
import controllers.console.spec as spec_module
from dify_app import DifyApp
from extensions import ext_login
from machinery.context import RequestContext
from services.schema_definition_service import SchemaDefinitionService
+from tests.helpers.unit.controllers.knowledge_fs_controller_fakes import narrowed
class TestSpecSchemaDefinitionsApi:
@@ -47,7 +50,11 @@ class TestSpecSchemaDefinitionsApi:
service.list.assert_called_once_with()
def test_get_documents_tight_response_model(self) -> None:
- response = spec_module.SpecSchemaDefinitionsApi.get.__apidoc__["responses"]["200"]
+ """Preconditions: The endpoint response schema is registered.
+
+ Postconditions: Its HTTP 200 documentation names the schema-definition response model.
+ """
+ response = vars(spec_module.SpecSchemaDefinitionsApi.get)["__apidoc__"]["responses"]["200"]
assert response[1].name == spec_module.SchemaDefinitionsResponse.__name__
@@ -74,6 +81,10 @@ class TestSpecSchemaDefinitionsApi:
assert resp == []
def test_get_rejects_unauthenticated_request_before_service_call(self) -> None:
+ """Preconditions: The request has no authenticated account.
+
+ Postconditions: The handler returns HTTP 401 without calling the schema service.
+ """
app = DifyApp(__name__)
app.config["TESTING"] = True
ext_login.init_app(app)
@@ -90,7 +101,7 @@ class TestSpecSchemaDefinitionsApi:
return_value=SimpleNamespace(schema_definitions=service),
),
):
- response = api.get()
+ response = narrowed(api.get(), Response)
assert response.status_code == 401
service.list.assert_not_called()
diff --git a/api/tests/unit_tests/controllers/console/workspace/test_human_input_node_data_migration.py b/api/tests/unit_tests/controllers/console/workspace/test_human_input_node_data_migration.py
index c5d1aa767ff..c400ab09ef3 100644
--- a/api/tests/unit_tests/controllers/console/workspace/test_human_input_node_data_migration.py
+++ b/api/tests/unit_tests/controllers/console/workspace/test_human_input_node_data_migration.py
@@ -588,7 +588,9 @@ def test_endpoint_preserves_auth_scope_decorators_and_rejects_before_service(
)
monkeypatch.setattr(wraps_module.dify_config, "RBAC_ENABLED", False)
- edit_wrapped_handler = NodeDataMigrationAPI.post.__wrapped__.__wrapped__.__wrapped__
+ edit_wrapped_handler = NodeDataMigrationAPI.post
+ for _ in range(3):
+ edit_wrapped_handler = vars(edit_wrapped_handler)["__wrapped__"]
with app.test_request_context(method="POST"), pytest.raises(Forbidden):
edit_wrapped_handler(NodeDataMigrationAPI())
diff --git a/api/tests/unit_tests/controllers/console/workspace/test_skills.py b/api/tests/unit_tests/controllers/console/workspace/test_skills.py
index daa530235c8..393da5ee8b9 100644
--- a/api/tests/unit_tests/controllers/console/workspace/test_skills.py
+++ b/api/tests/unit_tests/controllers/console/workspace/test_skills.py
@@ -85,8 +85,8 @@ def _recording_service(
return service, recorder
-def unwrap[ReturnT](func: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
- """Keep direct controller tests compatible with the session-injected methods."""
+def unwrap[ReturnT](func: Callable[..., object]) -> Callable[..., ReturnT]:
+ """Call the raw handler before session injection and response serialization."""
unwrapped = cast(Callable[..., ReturnT], inspect_unwrap(func))
parameters = list(signature(unwrapped).parameters.values())
if len(parameters) > 1 and parameters[1].name == "session":
diff --git a/api/tests/unit_tests/controllers/files/test_appdeploy_files.py b/api/tests/unit_tests/controllers/files/test_appdeploy_files.py
index c436341b797..d3d908f1ce9 100644
--- a/api/tests/unit_tests/controllers/files/test_appdeploy_files.py
+++ b/api/tests/unit_tests/controllers/files/test_appdeploy_files.py
@@ -5,13 +5,14 @@ from collections.abc import Callable, Iterator
from datetime import datetime
from io import BytesIO
from types import SimpleNamespace
-from typing import IO, cast
+from typing import IO, cast, overload
from unittest.mock import MagicMock, patch
from uuid import UUID
import jwt
import pytest
from flask import Flask
+from pydantic import TypeAdapter
from sqlalchemy import null, update
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
@@ -146,6 +147,31 @@ def _bearer(*scopes: FileGrantScope, end_user_id: str, tenant_id: str = TENANT_I
return {"Authorization": f"Bearer {token}"}
+type _UploadResource = GrantedFileUploadApi | GrantedRemoteFileUploadApi | ProducedFileApi
+type _UploadResult = tuple[dict[str, object], int]
+type _ResolveResult = dict[str, list[dict[str, object]]]
+
+
+@overload
+def _post_with_file_grant(resource: _UploadResource) -> _UploadResult: ...
+
+
+@overload
+def _post_with_file_grant(resource: GrantedFileResolveApi) -> _ResolveResult: ...
+
+
+def _post_with_file_grant(resource: _UploadResource | GrantedFileResolveApi) -> _UploadResult | _ResolveResult:
+ """Call the grant decorator and validate its raw response.
+
+ Preconditions: The resource owns a callable grant-decorated POST method.
+ Postconditions: Return its validated upload tuple or resolution dictionary.
+ Invalid response shapes raise a validation error. Grant admission is preserved.
+ """
+ post: Callable[..., object] = resource.post
+ result = post()
+ return TypeAdapter(_UploadResult | _ResolveResult).validate_python(result, strict=True)
+
+
def _content_token(*, file_id: str, kind: FileKind, expires_in: int = 300) -> str:
return jwt.encode(
{
@@ -227,7 +253,7 @@ def test_upload_stores_the_file_for_the_grant_end_user(
data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")},
content_type="multipart/form-data",
):
- body, status = GrantedFileUploadApi().post()
+ body, status = _post_with_file_grant(GrantedFileUploadApi())
assert status == 201
assert body["id"] == UPLOAD_FILE_ID
@@ -252,7 +278,7 @@ def test_upload_answers_in_dify_s_own_upload_shape(
data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")},
content_type="multipart/form-data",
):
- body, _ = GrantedFileUploadApi().post()
+ body, _ = _post_with_file_grant(GrantedFileUploadApi())
source_url = body.pop("source_url")
assert body == {
@@ -271,6 +297,7 @@ def test_upload_answers_in_dify_s_own_upload_shape(
"conversation_id": None,
"file_key": None,
}
+ assert isinstance(source_url, str)
assert source_url.startswith(f"https://files.example.com/files/appdeploy/{UPLOAD_FILE_ID}/content?token=")
@@ -285,7 +312,7 @@ def test_upload_carries_every_key_dify_s_web_client_reads(
data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")},
content_type="multipart/form-data",
):
- body, _ = GrantedFileUploadApi().post()
+ body, _ = _post_with_file_grant(GrantedFileUploadApi())
assert set(body) >= WEB_CLIENT_KEYS
@@ -300,7 +327,7 @@ def test_upload_rejects_a_grant_from_another_tenant(app: Flask, end_user: EndUse
content_type="multipart/form-data",
):
with pytest.raises(GrantedFileNotFoundError):
- GrantedFileUploadApi().post()
+ _post_with_file_grant(GrantedFileUploadApi())
@pytest.mark.usefixtures("sqlite_db")
@@ -317,7 +344,7 @@ def test_upload_applies_the_shared_per_extension_size_limit(
content_type="multipart/form-data",
):
with pytest.raises(FileTooLargeError):
- GrantedFileUploadApi().post()
+ _post_with_file_grant(GrantedFileUploadApi())
@pytest.mark.usefixtures("sqlite_db")
@@ -334,7 +361,7 @@ def test_upload_rejects_a_blacklisted_extension(
content_type="multipart/form-data",
):
with pytest.raises(BlockedFileExtensionError):
- GrantedFileUploadApi().post()
+ _post_with_file_grant(GrantedFileUploadApi())
@pytest.mark.usefixtures("sqlite_db")
@@ -347,7 +374,7 @@ def test_upload_rejects_a_request_carrying_no_file(app: Flask, end_user: EndUser
content_type="multipart/form-data",
):
with pytest.raises(NoFileUploadedError):
- GrantedFileUploadApi().post()
+ _post_with_file_grant(GrantedFileUploadApi())
@pytest.mark.usefixtures("sqlite_db")
@@ -363,7 +390,7 @@ def test_upload_rejects_a_batch_of_files(app: Flask, end_user: EndUser) -> None:
content_type="multipart/form-data",
):
with pytest.raises(TooManyFilesError):
- GrantedFileUploadApi().post()
+ _post_with_file_grant(GrantedFileUploadApi())
@pytest.mark.usefixtures("sqlite_db")
@@ -376,7 +403,7 @@ def test_upload_rejects_a_file_without_a_name(app: Flask, end_user: EndUser) ->
content_type="multipart/form-data",
):
with pytest.raises(FilenameNotExistsError):
- GrantedFileUploadApi().post()
+ _post_with_file_grant(GrantedFileUploadApi())
def test_remote_upload_fetches_through_the_ssrf_safe_fetcher(
@@ -400,7 +427,7 @@ def test_remote_upload_fetches_through_the_ssrf_safe_fetcher(
):
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
files_ns.payload = {"url": url}
- body, status = GrantedRemoteFileUploadApi().post()
+ body, status = _post_with_file_grant(GrantedRemoteFileUploadApi())
assert status == 201
assert body["id"] == UPLOAD_FILE_ID
@@ -437,11 +464,12 @@ def test_remote_upload_answers_in_the_upload_shape_plus_dify_s_url_key(
):
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
files_ns.payload = {"url": url}
- body, _ = GrantedRemoteFileUploadApi().post()
+ body, _ = _post_with_file_grant(GrantedRemoteFileUploadApi())
assert set(body) == DIFY_UPLOAD_RESPONSE_KEYS | {"url"}
# The row records where the bytes came from; the response hands back the
# signed URL that fetches them, exactly as dify's own remote upload does.
+ assert isinstance(body["source_url"], str)
assert body["source_url"].startswith(f"https://files.example.com/files/appdeploy/{UPLOAD_FILE_ID}/content?token=")
# One URL under both names, not two signings of the same file.
assert body["url"] == body["source_url"]
@@ -463,7 +491,7 @@ def test_remote_upload_honours_the_size_precheck(app: Flask, end_user: EndUser,
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
files_ns.payload = {"url": url}
with pytest.raises(FileTooLargeError):
- GrantedRemoteFileUploadApi().post()
+ _post_with_file_grant(GrantedRemoteFileUploadApi())
def test_produced_stores_a_tool_file_and_returns_both_urls(
@@ -484,13 +512,15 @@ def test_produced_stores_a_tool_file_and_returns_both_urls(
data={"file": (BytesIO(b"0" * 16), "chart.png")},
content_type="multipart/form-data",
):
- body, status = ProducedFileApi().post()
+ body, status = _post_with_file_grant(ProducedFileApi())
assert status == 201
kwargs = create_file.call_args.kwargs
assert kwargs["user_id"] == end_user.id
assert kwargs["tenant_id"] == TENANT_ID
assert kwargs["conversation_id"] is None
+ assert isinstance(body["url"], str)
+ assert isinstance(body["internal_url"], str)
assert body["url"].startswith(f"https://files.example.com/files/appdeploy/{tool_file.id}/content?token=")
assert body["internal_url"].startswith(
f"http://dify-api.dify.svc:5001/files/appdeploy/{tool_file.id}/content?token="
@@ -510,7 +540,7 @@ def test_produced_rejects_a_grant_whose_subject_was_deleted(app: Flask, file_gat
content_type="multipart/form-data",
):
with pytest.raises(GrantedFileNotFoundError):
- ProducedFileApi().post()
+ _post_with_file_grant(ProducedFileApi())
create_file.assert_not_called()
@@ -548,7 +578,7 @@ def test_produced_accepts_a_file_of_exactly_the_per_extension_limit(
data={"file": (BytesIO(b"0" * one_megabyte_image_limit), "chart.png")},
content_type="multipart/form-data",
):
- _, status = ProducedFileApi().post()
+ _, status = _post_with_file_grant(ProducedFileApi())
assert status == 201
assert len(create_file.call_args.kwargs["file_binary"]) == one_megabyte_image_limit
@@ -568,7 +598,7 @@ def test_produced_rejects_a_file_one_byte_over_the_per_extension_limit(
content_type="multipart/form-data",
):
with pytest.raises(FileTooLargeError) as raised:
- ProducedFileApi().post()
+ _post_with_file_grant(ProducedFileApi())
assert raised.value.code == 413
create_file.assert_not_called()
@@ -632,12 +662,14 @@ def test_resolve_signs_urls_per_item_and_hides_foreign_files(
):
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
files_ns.payload = payload
- body = GrantedFileResolveApi().post()
+ body = _post_with_file_grant(GrantedFileResolveApi())
resolved, hidden = body["files"]
assert resolved["ok"] is True
assert resolved["kind"] == "upload"
assert resolved["extension"] == "pdf"
+ assert isinstance(resolved["url"], str)
+ assert isinstance(resolved["internal_url"], str)
assert resolved["url"].startswith(f"https://files.example.com/files/appdeploy/{owned.id}/content?token=")
assert resolved["internal_url"].startswith(
f"http://dify-api.dify.svc:5001/files/appdeploy/{owned.id}/content?token="
@@ -679,7 +711,7 @@ def test_resolve_answers_a_mixed_batch_item_by_item(app: Flask, end_user: EndUse
):
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
files_ns.payload = payload
- body = GrantedFileResolveApi().post()
+ body = _post_with_file_grant(GrantedFileResolveApi())
assert [(file["id"], file["ok"], file["kind"], file["error"]) for file in body["files"]] == [
(owned_upload.id, True, "upload", None),
@@ -701,7 +733,7 @@ def test_resolve_returns_an_empty_batch_unchanged(app: Flask, end_user: EndUser)
):
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
files_ns.payload = payload
- assert GrantedFileResolveApi().post() == {"files": []}
+ assert _post_with_file_grant(GrantedFileResolveApi()) == {"files": []}
def test_resolve_rejects_an_unbounded_batch(app: Flask, end_user: EndUser, sqlite_db: FileGrantService) -> None:
@@ -721,7 +753,7 @@ def test_resolve_rejects_an_unbounded_batch(app: Flask, end_user: EndUser, sqlit
with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns:
files_ns.payload = payload
with pytest.raises(InvalidFileRequestError):
- GrantedFileResolveApi().post()
+ _post_with_file_grant(GrantedFileResolveApi())
@pytest.fixture
diff --git a/api/tests/unit_tests/controllers/files/test_tool_files.py b/api/tests/unit_tests/controllers/files/test_tool_files.py
index 0f18ef09925..000edfd8f55 100644
--- a/api/tests/unit_tests/controllers/files/test_tool_files.py
+++ b/api/tests/unit_tests/controllers/files/test_tool_files.py
@@ -1,6 +1,7 @@
from collections.abc import Iterator
from inspect import unwrap
from unittest.mock import MagicMock, patch
+from uuid import UUID
import pytest
from flask import Flask, Response
@@ -142,6 +143,6 @@ class TestToolFileApi:
with patch.object(module, "application_services") as application_services:
with app.test_request_context("/files/tools/file-id.txt"):
with pytest.raises(UnprocessableEntity):
- module.ToolFileApi().get("file-id", "txt")
+ module.ToolFileApi().get(UUID(int=1), "txt")
application_services.assert_not_called()
diff --git a/api/tests/unit_tests/controllers/openapi/test_files.py b/api/tests/unit_tests/controllers/openapi/test_files.py
index 97a04eec74d..832cf4cab95 100644
--- a/api/tests/unit_tests/controllers/openapi/test_files.py
+++ b/api/tests/unit_tests/controllers/openapi/test_files.py
@@ -1,11 +1,15 @@
+from inspect import unwrap
from io import BytesIO
from types import SimpleNamespace
from unittest.mock import Mock
+from uuid import UUID
import pytest
from flask import Flask
+from sqlalchemy.orm import Session
from werkzeug.exceptions import BadRequest
+from constants.oauth_bearer import TokenType
from controllers.common.errors import (
BlockedFileExtensionError,
FileTooLargeError,
@@ -14,8 +18,11 @@ from controllers.common.errors import (
from controllers.openapi._models import FileUploadPayload
from controllers.openapi._multipart import body_from_request
from controllers.openapi._upload import file_fields
+from controllers.openapi.auth.context import Context
+from controllers.openapi.auth.subjects import AccountSubject
from controllers.openapi.files import AppFileUploadApi
from libs.exception import BaseHTTPException
+from libs.oauth_bearer import AuthContext
from models import Account
from services.errors.file import BlockedFileExtensionError as ServiceBlockedFileExtensionError
from services.errors.file import FileTooLargeError as ServiceFileTooLargeError
@@ -24,10 +31,25 @@ from services.errors.file import UnsupportedFileTypeError as ServiceUnsupportedF
def _caller() -> Account:
caller = Account(name="Uploader", email="[email protected]")
- caller.id = "account-1"
+ caller.id = str(UUID(int=1))
return caller
+def _context(caller: Account, session: Session) -> Context:
+ auth = AuthContext(
+ token_type=TokenType.OAUTH_ACCOUNT,
+ subject_email=caller.email,
+ subject_issuer="dify:account",
+ account_id=UUID(caller.id),
+ client_id="difyctl",
+ token_id=UUID(int=2),
+ expires_at=None,
+ )
+ context = Context(AccountSubject(auth), session, {"app_id": "app-1"})
+ context._caller = caller
+ return context
+
+
def _upload_result() -> SimpleNamespace:
return SimpleNamespace(
id="00000000-0000-0000-0000-000000000001",
@@ -50,7 +72,9 @@ def _file_service(monkeypatch: pytest.MonkeyPatch) -> Mock:
return service
-def test_upload_uses_injected_file_service(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:
+def test_upload_uses_injected_file_service(
+ app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session
+) -> None:
service = _file_service(monkeypatch)
service.upload_file.return_value = _upload_result()
caller = _caller()
@@ -62,7 +86,7 @@ def test_upload_uses_injected_file_service(app: Flask, monkeypatch: pytest.Monke
content_type="multipart/form-data",
):
api = AppFileUploadApi()
- result = api.post.__handler__(api, SimpleNamespace(caller=caller, session=Mock()), app_id="app-1", body=_body())
+ result = unwrap(api.post)(api, _context(caller, sqlite_session), app_id="app-1", body=_body())
assert result.id == "00000000-0000-0000-0000-000000000001"
service.upload_file.assert_called_once_with(
@@ -96,6 +120,7 @@ def test_upload_uses_injected_file_service(app: Flask, monkeypatch: pytest.Monke
def test_upload_preserves_specific_file_errors(
app: Flask,
monkeypatch: pytest.MonkeyPatch,
+ sqlite_session: Session,
service_error: ValueError,
controller_error: type[BaseHTTPException],
status: int,
@@ -113,7 +138,7 @@ def test_upload_preserves_specific_file_errors(
):
api = AppFileUploadApi()
with pytest.raises(controller_error) as error_info:
- api.post.__handler__(api, SimpleNamespace(caller=_caller(), session=Mock()), app_id="app-1", body=_body())
+ unwrap(api.post)(api, _context(_caller(), sqlite_session), app_id="app-1", body=_body())
assert error_info.value.code == status
assert error_info.value.error_code == error_code
@@ -121,7 +146,9 @@ def test_upload_preserves_specific_file_errors(
assert error_info.value.__cause__ is service_error
-def test_upload_maps_other_value_errors_to_bad_request(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:
+def test_upload_maps_other_value_errors_to_bad_request(
+ app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session
+) -> None:
service = _file_service(monkeypatch)
service_error = ValueError("Filename contains invalid characters")
service.upload_file.side_effect = service_error
@@ -134,7 +161,7 @@ def test_upload_maps_other_value_errors_to_bad_request(app: Flask, monkeypatch:
):
api = AppFileUploadApi()
with pytest.raises(BadRequest) as error_info:
- api.post.__handler__(api, SimpleNamespace(caller=_caller(), session=Mock()), app_id="app-1", body=_body())
+ unwrap(api.post)(api, _context(_caller(), sqlite_session), app_id="app-1", body=_body())
assert error_info.value.description == str(service_error)
assert error_info.value.__cause__ is service_error
diff --git a/api/tests/unit_tests/controllers/service_api/dataset/test_dataset.py b/api/tests/unit_tests/controllers/service_api/dataset/test_dataset.py
index f94799f04d6..c27294cdd04 100644
--- a/api/tests/unit_tests/controllers/service_api/dataset/test_dataset.py
+++ b/api/tests/unit_tests/controllers/service_api/dataset/test_dataset.py
@@ -4,6 +4,7 @@ from inspect import unwrap
from types import SimpleNamespace
from typing import Literal, cast
from unittest.mock import MagicMock, patch
+from uuid import UUID
import pytest
from flask import Flask, request
@@ -820,7 +821,7 @@ class TestDatasetApisCases:
api = DocumentStatusApi()
response, status = api.patch(
tenant_id=tenant.id,
- dataset_id=dataset.id,
+ dataset_id=UUID(dataset.id),
action="enable",
)
@@ -848,7 +849,7 @@ class TestDatasetApisCases:
with pytest.raises(NotFound):
api.patch(
tenant_id=tenant.id,
- dataset_id=dataset.id,
+ dataset_id=UUID(dataset.id),
action="enable",
)
@@ -876,7 +877,7 @@ class TestDatasetApisCases:
with pytest.raises(Forbidden):
api.patch(
tenant_id=tenant.id,
- dataset_id=dataset.id,
+ dataset_id=UUID(dataset.id),
action="enable",
)
@@ -906,7 +907,7 @@ class TestDatasetApisCases:
with pytest.raises(InvalidActionError):
api.patch(
tenant_id=tenant.id,
- dataset_id=dataset.id,
+ dataset_id=UUID(dataset.id),
action="enable",
)
@@ -936,7 +937,7 @@ class TestDatasetApisCases:
with pytest.raises(InvalidActionError):
api.patch(
tenant_id=tenant.id,
- dataset_id=dataset.id,
+ dataset_id=UUID(dataset.id),
action="enable",
)
diff --git a/api/tests/unit_tests/controllers/service_api/dataset/test_hit_testing.py b/api/tests/unit_tests/controllers/service_api/dataset/test_hit_testing.py
index 5c853e53ddc..896b6a44094 100644
--- a/api/tests/unit_tests/controllers/service_api/dataset/test_hit_testing.py
+++ b/api/tests/unit_tests/controllers/service_api/dataset/test_hit_testing.py
@@ -15,10 +15,12 @@ Strategy:
"""
import uuid
+from typing import TypedDict
from unittest.mock import MagicMock, patch
import pytest
from flask import Flask, g
+from pydantic import TypeAdapter
from werkzeug.exceptions import Forbidden, NotFound
import services.errors.base
@@ -26,6 +28,7 @@ from controllers.service_api.dataset.hit_testing import HitTestingApi, HitTestin
from models.account import Account, Tenant, TenantAccountRole
from models.dataset import Dataset
from services.knowledge.entities.knowledge_entities import RetrievalModel
+from tests.helpers.unit.controllers.knowledge_fs_controller_fakes import narrowed
# ---------------------------------------------------------------------------
# HitTestingPayload Model Tests
@@ -71,6 +74,15 @@ def hit_testing_record() -> dict[str, object]:
}
+class _HitRecord(TypedDict):
+ segment: dict[str, object]
+ child_chunks: list[object]
+ files: list[object]
+ score: float
+ tsne_position: list[float] | None
+ summary: str | None
+
+
class TestHitTestingPayload:
"""Test suite for HitTestingPayload Pydantic model."""
@@ -151,7 +163,10 @@ class TestHitTestingApiPost:
mock_ns: MagicMock,
app: Flask,
) -> None:
- """Test successful hit testing request."""
+ """Preconditions: The dataset exists and the account is admitted.
+
+ Postconditions: The handler returns the query and invokes retrieval.
+ """
dataset_id = str(uuid.uuid4())
tenant_id = str(uuid.uuid4())
@@ -171,7 +186,7 @@ class TestHitTestingApiPost:
g._login_user = account
api = HitTestingApi()
# Skip billing decorator via __wrapped__
- response = HitTestingApi.post.__wrapped__(api, tenant_id, dataset_id)
+ response = vars(HitTestingApi.post)["__wrapped__"](api, tenant_id, dataset_id)
assert response["query"] == {"content": "test query"}
mock_hit_svc.retrieve.assert_called_once()
@@ -186,7 +201,10 @@ class TestHitTestingApiPost:
mock_ns: MagicMock,
app: Flask,
) -> None:
- """Test hit testing with custom retrieval model."""
+ """Preconditions: The request supplies a valid retrieval model for an accessible dataset.
+
+ Postconditions: The handler passes that model to retrieval and returns its response.
+ """
dataset_id = str(uuid.uuid4())
tenant_id = str(uuid.uuid4())
@@ -217,7 +235,7 @@ class TestHitTestingApiPost:
# TODO: the service APIs are NOT migrated yet, so we have to do the very dirty hack
g._login_user = account
api = HitTestingApi()
- response = HitTestingApi.post.__wrapped__(api, tenant_id, dataset_id)
+ response = vars(HitTestingApi.post)["__wrapped__"](api, tenant_id, dataset_id)
assert response["query"] == {"content": "complex query"}
call_kwargs = mock_hit_svc.retrieve.call_args
@@ -237,7 +255,10 @@ class TestHitTestingApiPost:
mock_ns: MagicMock,
app: Flask,
) -> None:
- """Service API retrieval payload should not drop metadata filters."""
+ """Preconditions: The retrieval model includes metadata filters.
+
+ Postconditions: The handler preserves those filters in the retrieval request.
+ """
dataset_id = str(uuid.uuid4())
tenant_id = str(uuid.uuid4())
@@ -274,7 +295,7 @@ class TestHitTestingApiPost:
# TODO: the service APIs are NOT migrated yet, so we have to do the very dirty hack
g._login_user = account
api = HitTestingApi()
- HitTestingApi.post.__wrapped__(api, tenant_id, dataset_id)
+ vars(HitTestingApi.post)["__wrapped__"](api, tenant_id, dataset_id)
passed_retrieval_model = mock_hit_svc.retrieve.call_args.kwargs.get("retrieval_model")
assert passed_retrieval_model is not None
@@ -290,7 +311,10 @@ class TestHitTestingApiPost:
mock_ns: MagicMock,
app: Flask,
) -> None:
- """Test service API prepares nullable list fields from retrieval records."""
+ """Preconditions: The retrieval result contains nullable list fields.
+
+ Postconditions: The serialized record contains empty lists and preserves its remaining fields.
+ """
dataset_id = str(uuid.uuid4())
tenant_id = str(uuid.uuid4())
@@ -312,10 +336,10 @@ class TestHitTestingApiPost:
# TODO: the service APIs are NOT migrated yet, so we have to do the very dirty hack
g._login_user = account
api = HitTestingApi()
- response = HitTestingApi.post.__wrapped__(api, tenant_id, dataset_id)
+ response = vars(HitTestingApi.post)["__wrapped__"](api, tenant_id, dataset_id)
assert response["query"] == {"content": "legacy query"}
- record = response["records"][0]
+ record = TypeAdapter(_HitRecord).validate_python(narrowed(response["records"], list)[0], strict=True)
assert record["segment"]["id"] == "segment-1"
assert record["segment"]["keywords"] == []
assert record["child_chunks"] == []
@@ -332,7 +356,10 @@ class TestHitTestingApiPost:
mock_ns: MagicMock,
app: Flask,
) -> None:
- """Test hit testing with non-existent dataset."""
+ """Preconditions: The dataset lookup reports a missing dataset.
+
+ Postconditions: The handler raises NotFound.
+ """
dataset_id = str(uuid.uuid4())
tenant_id = str(uuid.uuid4())
account = self._account(tenant_id)
@@ -345,7 +372,7 @@ class TestHitTestingApiPost:
g._login_user = account
api = HitTestingApi()
with pytest.raises(NotFound):
- HitTestingApi.post.__wrapped__(api, tenant_id, dataset_id)
+ vars(HitTestingApi.post)["__wrapped__"](api, tenant_id, dataset_id)
@patch("controllers.service_api.dataset.hit_testing.service_api_ns")
@patch("controllers.console.datasets.hit_testing_base.DatasetService")
@@ -355,7 +382,10 @@ class TestHitTestingApiPost:
mock_ns: MagicMock,
app: Flask,
) -> None:
- """Test hit testing when user lacks dataset permission."""
+ """Preconditions: The account has no permission for the dataset.
+
+ Postconditions: The handler raises Forbidden.
+ """
dataset_id = str(uuid.uuid4())
tenant_id = str(uuid.uuid4())
@@ -371,4 +401,4 @@ class TestHitTestingApiPost:
g._login_user = account
api = HitTestingApi()
with pytest.raises(Forbidden):
- HitTestingApi.post.__wrapped__(api, tenant_id, dataset_id)
+ vars(HitTestingApi.post)["__wrapped__"](api, tenant_id, dataset_id)
diff --git a/api/tests/unit_tests/controllers/service_api/dataset/test_metadata.py b/api/tests/unit_tests/controllers/service_api/dataset/test_metadata.py
index 8ced1d1c983..57a0a28cc7e 100644
--- a/api/tests/unit_tests/controllers/service_api/dataset/test_metadata.py
+++ b/api/tests/unit_tests/controllers/service_api/dataset/test_metadata.py
@@ -224,7 +224,7 @@ class TestDatasetMetadataCreatePost(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
)
assert status == 201
@@ -254,7 +254,7 @@ class TestDatasetMetadataCreatePost(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
)
@@ -286,7 +286,7 @@ class TestDatasetMetadataCreateGet(_UsesSQLiteSession):
api = DatasetMetadataCreateServiceApi()
response, status = api.get(
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
)
assert status == 200
@@ -312,7 +312,7 @@ class TestDatasetMetadataCreateGet(_UsesSQLiteSession):
):
api = DatasetMetadataCreateServiceApi()
with pytest.raises(NotFound):
- api.get(tenant_id=mock_tenant.id, dataset_id=mock_dataset.id)
+ api.get(tenant_id=mock_tenant.id, dataset_id=uuid.UUID(mock_dataset.id))
# ---------------------------------------------------------------------------
@@ -362,7 +362,7 @@ class TestDatasetMetadataServiceApiPatch(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
metadata_id=metadata_id,
)
@@ -399,7 +399,7 @@ class TestDatasetMetadataServiceApiPatch(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
metadata_id=metadata_id,
)
@@ -441,7 +441,7 @@ class TestDatasetMetadataServiceApiDelete(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
metadata_id=metadata_id,
)
@@ -474,7 +474,7 @@ class TestDatasetMetadataServiceApiDelete(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
metadata_id=metadata_id,
)
@@ -508,7 +508,7 @@ class TestDatasetMetadataBuiltInFieldGet(_UsesSQLiteSession):
api = DatasetMetadataBuiltInFieldServiceApi()
response, status = api.get(
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
)
assert status == 200
@@ -557,7 +557,7 @@ class TestDatasetMetadataBuiltInFieldAction(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
action="enable",
)
@@ -590,7 +590,7 @@ class TestDatasetMetadataBuiltInFieldAction(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
action="disable",
)
@@ -619,7 +619,7 @@ class TestDatasetMetadataBuiltInFieldAction(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
action="enable",
)
@@ -670,7 +670,7 @@ class TestDocumentMetadataEditPost(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
)
assert status == 200
@@ -704,7 +704,7 @@ class TestDocumentMetadataEditPost(_UsesSQLiteSession):
api,
session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
)
@patch("controllers.service_api.dataset.metadata.application_services")
@@ -732,7 +732,7 @@ class TestDocumentMetadataEditPost(_UsesSQLiteSession):
api,
self.session,
tenant_id=mock_tenant.id,
- dataset_id=mock_dataset.id,
+ dataset_id=uuid.UUID(mock_dataset.id),
)
assert exc_info.value.description == "Document not found."
diff --git a/api/uv.lock b/api/uv.lock
index 8e2887ee428..e5d26e2f3cd 100644
--- a/api/uv.lock
+++ b/api/uv.lock
@@ -1794,7 +1794,7 @@ requires-dist = [
{ name = "flask-login", specifier = "==0.6.3" },
{ name = "flask-migrate", specifier = ">=4.1.0,<5.0.0" },
{ name = "flask-orjson", specifier = ">=2.0.0,<3.0.0" },
- { name = "flask-restx", git = "https://github.com/asukaminato0721/flask-restx?rev=27758e26f8f740d7525d5039c51a9e524b6e2b68" },
+ { name = "flask-restx", git = "https://github.com/asukaminato0721/flask-restx?rev=e44a2861b854" },
{ name = "gevent", specifier = ">=26.4.0,<26.5.0" },
{ name = "gevent-websocket", specifier = "==0.10.1" },
{ name = "gmpy2", specifier = ">=2.3.0,<3.0.0" },
@@ -2763,7 +2763,7 @@ wheels = [
[[package]]
name = "flask-restx"
version = "1.3.3.dev0"
-source = { git = "https://github.com/asukaminato0721/flask-restx?rev=27758e26f8f740d7525d5039c51a9e524b6e2b68#27758e26f8f740d7525d5039c51a9e524b6e2b68" }
+source = { git = "https://github.com/asukaminato0721/flask-restx?rev=e44a2861b854#e44a2861b854e672625c196f79debb257d3c846d" }
dependencies = [
{ name = "aniso8601" },
{ name = "flask" },