From 2dbe3b8a6227c769df50a837f8799fecbbb1ba6a Mon Sep 17 00:00:00 2001 From: Hamza Amin Khokhar <137163270+hak2979@users.noreply.github.com> Date: Mon, 18 May 2026 15:54:30 +0500 Subject: [PATCH] fix: metadata_condition returning all docs when filter matches nothing (#14967) ### What problem does this PR solve? When _parse_doc_id_filter_with_metadata returns [], the empty list is falsy so the WHERE id IN (...) clause was silently skipped, causing the full dataset to be returned instead of an empty result. Change `if doc_ids:` to `if doc_ids is not None:` in both get_list() and get_by_kb_id() to distinguish between no filter (None) and a filter that matched zero documents ([]). Fixes #14962 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --- api/apps/restful_apis/document_api.py | 2 +- api/db/services/document_service.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/api/apps/restful_apis/document_api.py b/api/apps/restful_apis/document_api.py index 7547d2f20b..57215080d3 100644 --- a/api/apps/restful_apis/document_api.py +++ b/api/apps/restful_apis/document_api.py @@ -995,7 +995,7 @@ def _parse_doc_id_filter_with_metadata(req, kb_id): if not doc_ids_filter: return RetCode.SUCCESS, "", [], return_empty_metadata - return RetCode.SUCCESS, "", list(doc_ids_filter) if doc_ids_filter is not None else [], return_empty_metadata + return RetCode.SUCCESS, "", list(doc_ids_filter) if doc_ids_filter is not None else None, return_empty_metadata @manager.route("/datasets//documents", methods=["DELETE"]) # noqa: F821 diff --git a/api/db/services/document_service.py b/api/db/services/document_service.py index 3d9bc09dbb..7b0bce6e62 100644 --- a/api/db/services/document_service.py +++ b/api/db/services/document_service.py @@ -88,7 +88,7 @@ class DocumentService(CommonService): docs = docs.where(cls.model.name == name) if keywords: docs = docs.where(fn.LOWER(cls.model.name).contains(keywords.lower())) - if doc_ids: + if doc_ids is not None: docs = docs.where(cls.model.id.in_(doc_ids)) if suffix: docs = docs.where(cls.model.suffix.in_(suffix)) @@ -143,7 +143,7 @@ class DocumentService(CommonService): .join(User, on=(cls.model.created_by == User.id), join_type=JOIN.LEFT_OUTER) .where(cls.model.kb_id == kb_id) ) - if doc_ids: + if doc_ids is not None: docs = docs.where(cls.model.id.in_(doc_ids)) if run_status: docs = docs.where(cls.model.run.in_(run_status))