fix(api): honor include_parsing_status in List Datasets endpoint (#16913)

Fixes #16855.

## Problem

`ListDatasetReq` declares `include_parsing_status: bool = False` but
`dataset_api_service.list_datasets` ignored the flag, so callers passing
`include_parsing_status=true` got `null` for every parsing-status field.
The helper that produces the counts
(`DocumentService.get_parsing_status_by_kb_ids`) already existed and
returned the documented shape; it just wasn't being called.

## Fix

Read the flag in `list_datasets`, call the helper only when truthy, and
attach per-kb counts to each record before serialisation. The flag
follows the same string-coercion idiom already used for `desc` in this
function.

When the flag is false or absent, no new field is added to the response,
so the change is non-breaking and additive. Existing callers see
byte-identical responses.

## Tests

New file
`test/unit_test/api/apps/services/test_dataset_api_service_list_datasets.py`
(7 tests, all passing):

-
`test_list_datasets_without_include_parsing_status_does_not_call_helper`
- `test_list_datasets_with_include_parsing_status_true_attaches_counts`
- `test_list_datasets_with_include_parsing_status_string_true`
- `test_list_datasets_with_include_parsing_status_false_skips_helper`
-
`test_list_datasets_with_include_parsing_status_string_false_skips_helper`
-
`test_list_datasets_with_empty_kb_list_skips_helper_even_when_flag_true`
-
`test_list_datasets_with_include_parsing_status_missing_kb_gets_empty_dict`

```
$ uv run pytest test/unit_test/api/apps/services/test_dataset_api_service_list_datasets.py -v
============================== 7 passed in 0.19s ==============================
```

## Files changed

- `api/apps/services/dataset_api_service.py` (+15)
-
`test/unit_test/api/apps/services/test_dataset_api_service_list_datasets.py`
(new)

## Follow-up (out of scope)

Register the `parsing_status` field in the OpenAPI response schema at
`api/apps/restful_apis/dataset_api.py` so Swagger / generated clients
document it.

Co-authored-by: Harsh23Kashyap <harsh@example.com>
This commit is contained in:
Harsh Kashyap
2026-07-18 18:20:00 +05:30
committed by GitHub
parent afc12b2bff
commit cafbbd467d
2 changed files with 361 additions and 0 deletions

View File

@@ -409,10 +409,25 @@ def list_datasets(tenant_id: str, args: dict):
kbs, total = KnowledgebaseService.get_list(tenant_ids, tenant_id, page, page_size, orderby, desc, kb_id, name, keywords, parser_id)
users = UserService.get_by_ids([m["tenant_id"] for m in kbs])
user_map = {m.id: m.to_dict() for m in users}
ips_arg = args.get("include_parsing_status", False)
if isinstance(ips_arg, str):
include_parsing_status = ips_arg.lower() not in ("false", "0", "")
elif isinstance(ips_arg, bool):
include_parsing_status = ips_arg
else:
include_parsing_status = bool(ips_arg)
status_by_kb = {}
if include_parsing_status and kbs:
status_by_kb = DocumentService.get_parsing_status_by_kb_ids([kb["id"] for kb in kbs])
response_data_list = []
for kb in kbs:
user_dict = user_map.get(kb["tenant_id"], {})
kb.update({"nickname": user_dict.get("nickname", ""), "tenant_avatar": user_dict.get("avatar", "")})
if status_by_kb:
kb["parsing_status"] = status_by_kb.get(kb["id"], {})
response_data_list.append(remap_dictionary_keys(kb))
return True, {"data": response_data_list, "total": total}