fix(api): update-traits returns 400 for malformed request body - #8199
fix(api): update-traits returns 400 for malformed request body#8199bardock-2393 wants to merge 1 commit into
Conversation
Sending a non-object payload (e.g. a JSON array) to the edge identity update-traits endpoint unpacked request.data directly into TraitModel, raising an uncaught TypeError and a 500 instead of a validation error.
|
@bardock-2393 is attempting to deploy a commit to the Flagsmith Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe Estimated code review effort: 2 (Simple) | ~10 minutes ✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
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 |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8199 +/- ##
==========================================
- Coverage 98.71% 98.57% -0.15%
==========================================
Files 1531 1531
Lines 61241 61267 +26
==========================================
- Hits 60453 60392 -61
- Misses 788 875 +87 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
docs/if required so people know about the feature.Changes
Closes #7951
The edge identity
update-traitsendpoint (PUT /api/v1/environments/<api_key>/edge-identities/<uuid>/update-traits/) crashed with a 500 error instead of returning a 400 when the request body wasn't a JSON object — for example, sending a JSON array as the payload.This happened because the view unpacked the request body directly into a pydantic model (
TraitModel(**request.data)). Whenrequest.datawas a list, Python raised aTypeErroron the**unpacking itself, before pydantic ever got a chance to validate anything, so the existingexcept pydantic.ValidationErrorhandler never caught it.The fix adds an explicit check that the request body is a JSON object before unpacking it, returning a standard 400 validation error otherwise — consistent with how the endpoint already reports other validation failures.
How did you test this code?
Added a test (
test_edge_identities_update_trait__malformed_payload__returns_400) that sends a JSON array as the request body to the update-traits endpoint and asserts a 400 response. I confirmed this test reproduces the original crash on the unpatched code (fails with the exactTypeErrorfrom the linked Sentry issue) and passes with the fix applied.Ran the full test module for this view (
api/tests/integration/edge_api/identities/test_edge_identity_viewset.py, 22 tests) against a local Postgres database — all passing. Also ranruff(lint + format) andmypyon the changed files with no issues.