Repository navigation
Conversation
update_document_name_only() accessed file.id without checking FileService.get_by_id() when File2Document pointed at a deleted file. Skip the optional file rename and still update the document index. Fixes infiniflow#15534
📝 WalkthroughWalkthroughThe PR fixes a crash in document rename when a File2Document row points to a deleted file. The service method now guards the file update with a null-check, and comprehensive regression tests validate both the skip-when-missing and update-when-present code paths. ChangesDocument Rename Orphan Link Guard
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
api/apps/services/document_api_service.py (1)
41-43: 💤 Low valueGuard correctly prevents the orphan-link crash; consider logging the skip.
The
if e and file:guard fixes theAttributeErroras intended. Since this introduces a new silent branch (skipping the file rename when theFile2Documentmapping points to a deletedFile), a log line here would aid future debugging of orphan links.loggingis already imported.♻️ Optional: log when the linked file is missing
e, file = FileService.get_by_id(informs[0].file_id) if e and file: FileService.update_by_id(file.id, {"name": req_doc_name}) + else: + logging.warning( + "Skipping file rename for document %s: linked file %s not found (orphan File2Document link).", + document_id, + informs[0].file_id, + )As per coding guidelines: "Add logging for new flows".
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/apps/services/document_api_service.py` around lines 41 - 43, The guard added around FileService.get_by_id (checking e and file) prevents an AttributeError but silently skips renaming when the File2Document link is orphaned; add a logging call to record this path. Inside the branch where you currently do nothing (the else of "if e and file:"), emit a log (using the existing logging import) that includes the document id or informs[0].file_id and req_doc_name to indicate the missing File and that the rename was skipped; keep the successful path unchanged where FileService.update_by_id(file.id, {"name": req_doc_name}) is called.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@api/apps/services/document_api_service.py`:
- Around line 41-43: The guard added around FileService.get_by_id (checking e
and file) prevents an AttributeError but silently skips renaming when the
File2Document link is orphaned; add a logging call to record this path. Inside
the branch where you currently do nothing (the else of "if e and file:"), emit a
log (using the existing logging import) that includes the document id or
informs[0].file_id and req_doc_name to indicate the missing File and that the
rename was skipped; keep the successful path unchanged where
FileService.update_by_id(file.id, {"name": req_doc_name}) is called.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8f8ac2e2-7997-4786-b663-7311f59a1881
📒 Files selected for processing (2)
api/apps/services/document_api_service.pytest/unit_test/api/apps/services/test_update_document_name_only.py
|
@yingfeng |
|
@wangq8 , @Haruko386 , @JinHai-CN @yingfeng @xugangqiang |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15536 +/- ##
==========================================
- Coverage 94.56% 93.16% -1.40%
==========================================
Files 10 10
Lines 717 717
Branches 118 118
==========================================
- Hits 678 668 -10
- Misses 25 29 +4
- Partials 14 20 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
LGTM |
Summary
Fixes #15534 —
update_document_name_only()crashes withAttributeErrorwhenFile2Documentexists but the linkedFilerow was deleted.update_document_name_only()indocument_api_service.pycalledFileService.get_by_id()when aFile2Documentrow existed, then accessedfile.idwithout checking the lookup result. An orphanFile2Documentlink (file deleted, mapping left behind) caused document rename viaPATCH /api/v1/datasets/{dataset_id}/documents/{document_id}to return HTTP 500.This PR mirrors guards used in
file2document_api.pyandfile_api_service.py: skip the optional file rename when the file is missing, and still update the document record and search index.Changes
api/apps/services/document_api_service.py— checke and filebeforeFileService.update_by_idtest/unit_test/api/apps/services/test_update_document_name_only.py— regression tests (orphan link + happy path)Test plan
pytest test/unit_test/api/apps/services/test_update_document_name_only.py -vnamewhenFile2Documentpoints to a non-existentfile_id→ 200, document/index renamed, no 500