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.

refactor(controllers/console): inject saved-message query with @model_validate (#41819)

jiao-weiran3f76dbe67a08
diff --git a/api/controllers/console/explore/saved_message.py b/api/controllers/console/explore/saved_message.py
index 7662f9e3ac3..7facb7fd530 100644
--- a/api/controllers/console/explore/saved_message.py
+++ b/api/controllers/console/explore/saved_message.py
@@ -1,6 +1,5 @@
from uuid import UUID
-from flask import request
from pydantic import TypeAdapter
from werkzeug.exceptions import NotFound
@@ -28,7 +27,8 @@ class SavedMessageListApi(InstalledAppResource):
@console_ns.doc(params=query_params_from_model(SavedMessageListQuery))
@console_ns.response(200, "Success", console_ns.models[SavedMessageInfiniteScrollPagination.__name__])
@with_current_user
- def get(self, current_user: Account, installed_app: InstalledApp):
+ @model_validate(SavedMessageListQuery)
+ def get(self, req_data: SavedMessageListQuery, current_user: Account, installed_app: InstalledApp):
session = db.session()
app_model = installed_app.app_with_session(session=session)
if app_model is None:
@@ -36,10 +36,12 @@ class SavedMessageListApi(InstalledAppResource):
if app_model.mode != "completion":
raise NotCompletionAppError()
- args = SavedMessageListQuery.model_validate(request.args.to_dict())
-
pagination = SavedMessageService.pagination_by_last_id(
- app_model, current_user, str(args.last_id) if args.last_id else None, args.limit, session=session
+ app_model,
+ current_user,
+ str(req_data.last_id) if req_data.last_id else None,
+ req_data.limit,
+ session=session,
)
adapter = TypeAdapter(SavedMessageItem)
items = [
diff --git a/api/tests/unit_tests/controllers/console/explore/test_saved_message.py b/api/tests/unit_tests/controllers/console/explore/test_saved_message.py
index 3ca6b9d179b..a55500690be 100644
--- a/api/tests/unit_tests/controllers/console/explore/test_saved_message.py
+++ b/api/tests/unit_tests/controllers/console/explore/test_saved_message.py
@@ -134,7 +134,7 @@ class TestSavedMessageListApi(_UsesSQLiteSession):
return_value=pagination,
) as pagination_mock,
):
- result = method(api, self.account, installed_app)
+ result = method(api, module.SavedMessageListQuery.model_validate({}), self.account, installed_app)
pagination_mock.assert_called_once()
assert pagination_mock.call_args.args[1] is self.account
@@ -142,6 +142,41 @@ class TestSavedMessageListApi(_UsesSQLiteSession):
assert result["has_more"] is False
assert len(result["data"]) == 2
+ def test_get_with_query_params(self, app: Flask):
+ api = module.SavedMessageListApi()
+ method = unwrap(api.get)
+
+ installed_app = make_installed_app(self.sqlite_session, mode=AppMode.COMPLETION)
+
+ pagination = MagicMock(
+ limit=50,
+ has_more=True,
+ data=[
+ make_message(self.sqlite_session, app_id=installed_app.app_id, account_id=self.account.id),
+ ],
+ )
+ self.sqlite_session.commit()
+
+ last_id = str(uuid4())
+ query = module.SavedMessageListQuery.model_validate({"last_id": last_id, "limit": 50})
+
+ with (
+ app.test_request_context("/", query_string={"last_id": last_id, "limit": "50"}),
+ patch.object(
+ module.SavedMessageService,
+ "pagination_by_last_id",
+ return_value=pagination,
+ ) as pagination_mock,
+ ):
+ result = method(api, query, self.account, installed_app)
+
+ pagination_mock.assert_called_once()
+ assert pagination_mock.call_args.args[1] is self.account
+ assert pagination_mock.call_args.args[2] == last_id
+ assert pagination_mock.call_args.args[3] == 50
+ assert result["limit"] == 50
+ assert result["has_more"] is True
+
def test_get_not_completion_app(self):
api = module.SavedMessageListApi()
method = unwrap(api.get)
@@ -149,7 +184,7 @@ class TestSavedMessageListApi(_UsesSQLiteSession):
installed_app = make_installed_app(self.sqlite_session, mode=AppMode.CHAT)
with pytest.raises(NotCompletionAppError):
- method(api, self.account, installed_app)
+ method(api, module.SavedMessageListQuery.model_validate({}), self.account, installed_app)
def test_post_success(self, app: Flask, payload_patch):
api = module.SavedMessageListApi()