fix: add metadata JSON parsing with error handling in get_cluster_images#1344
fix: add metadata JSON parsing with error handling in get_cluster_images#1344Manasvibansal1 wants to merge 2 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
💤 Files with no reviewable changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughFace cluster image metadata is now parsed from JSON before being returned by the API, with invalid or missing metadata mapped to ChangesFace cluster metadata handling
Sequence Diagram(s)Estimated Code Review Effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
backend/tests/test_face_clusters.py (1)
442-534: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winRemove the duplicated metadata tests.
These methods duplicate the ones above with the same names, so Python keeps only the later definitions. The extra block is dead code, and the second GET in
test_null_metadata_handleddoes not add new coverage.Suggested cleanup
- # ============================================================================ - # Metadata JSON Parsing Tests (Fix for Issue `#705`) - # ============================================================================ - - `@patch`("app.routes.face_clusters.db_get_images_by_cluster_id") - `@patch`("app.routes.face_clusters.db_get_cluster_by_id") - def test_metadata_string_is_parsed(self, mock_get_cluster, mock_get_images): - ... - - `@patch`("app.routes.face_clusters.db_get_images_by_cluster_id") - `@patch`("app.routes.face_clusters.db_get_cluster_by_id") - def test_metadata_dict_passes_through(self, mock_get_cluster, mock_get_images): - ... - - `@patch`("app.routes.face_clusters.db_get_images_by_cluster_id") - `@patch`("app.routes.face_clusters.db_get_cluster_by_id") - def test_invalid_metadata_returns_none(self, mock_get_cluster, mock_get_images): - ... - `@patch`("app.routes.face_clusters.db_get_images_by_cluster_id") `@patch`("app.routes.face_clusters.db_get_cluster_by_id") def test_null_metadata_handled(self, mock_get_cluster, mock_get_images): """None metadata from database should be handled safely.""" @@ response = client.get("/face_clusters/cluster_123/images") assert response.status_code == 200 assert response.json()["data"]["images"][0]["metadata"] is None - - response = client.get("/face_clusters/cluster_123/images") - - assert response.status_code == 200 - assert response.json()["data"]["images"][0]["metadata"] is None🤖 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 `@backend/tests/test_face_clusters.py` around lines 442 - 534, The metadata parsing tests in test_face_clusters are duplicated, and the later definitions override the earlier ones, leaving dead code. Remove the repeated test methods in the face cluster metadata section, especially the duplicate test_metadata_string_is_parsed, test_metadata_dict_passes_through, test_invalid_metadata_returns_none, and test_null_metadata_handled blocks, and keep only one version of each test in the test class. Also drop the extra repeated client.get call inside test_null_metadata_handled since it adds no coverage.Source: Path instructions
🤖 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.
Inline comments:
In `@backend/app/routes/face_clusters.py`:
- Around line 181-195: The parse_metadata helper in the image cluster response
should normalize only object-shaped JSON for ImageInCluster.metadata. Update
parse_metadata so that when metadata is a string it returns the parsed value
only if it is a dict/object, and otherwise returns None for arrays, scalars, or
invalid JSON. Keep the ImageInCluster construction using parse_metadata so
metadata stays within dict | None and does not fall through to the 500 handler.
---
Nitpick comments:
In `@backend/tests/test_face_clusters.py`:
- Around line 442-534: The metadata parsing tests in test_face_clusters are
duplicated, and the later definitions override the earlier ones, leaving dead
code. Remove the repeated test methods in the face cluster metadata section,
especially the duplicate test_metadata_string_is_parsed,
test_metadata_dict_passes_through, test_invalid_metadata_returns_none, and
test_null_metadata_handled blocks, and keep only one version of each test in the
test class. Also drop the extra repeated client.get call inside
test_null_metadata_handled since it adds no coverage.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 6de7e23a-c9c7-4c7e-9aa2-0cb227350630
📒 Files selected for processing (4)
backend/app/database/face_clusters.pybackend/app/routes/face_clusters.pybackend/test_db.sqlite3backend/tests/test_face_clusters.py
Fixes #705
Problem
The GET /face-clusters/{id}/images endpoint was crashing with 500 error
when metadata was stored as a JSON string instead of a dict. Invalid JSON
metadata also caused unhandled crashes.
Changes Made
parse_metadata()helper inapp/routes/face_clusters.pytosafely parse metadata strings into dicts
app/database/face_clusters.pyTests Added
4 new tests in
tests/test_face_clusters.py:test_metadata_string_is_parsed: string metadata parsed to dicttest_metadata_dict_passes_through: dict metadata stays unchangedtest_invalid_metadata_returns_none: broken JSON returns None safelytest_null_metadata_handled— None metadata handled safelyTest Results
All 30 tests passing
Summary by CodeRabbit
Bug Fixes
nullfor metadata when it can’t be decoded or isn’t a valid object.Tests