Skip to content

fix: prevent 500 error on partial update of multivariate options - #8309

Open
srijantrpth wants to merge 7 commits into
Flagsmith:mainfrom
srijantrpth:fix-8306-partial-update-multivariate
Open

fix: prevent 500 error on partial update of multivariate options#8309
srijantrpth wants to merge 7 commits into
Flagsmith:mainfrom
srijantrpth:fix-8306-partial-update-multivariate

Conversation

@srijantrpth

Copy link
Copy Markdown
Contributor

Thanks for submitting a PR! Please check the boxes below:

  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Closes #8306

Resolved an issue where partially updating a multivariate option via a PATCH request returned a 500 Internal Server Error when omitting fields like default_percentage_allocation or feature.

Updated MultivariateFeatureOptionSerializer.validate() and _validate_key_is_unique() in api/features/multivariate/serializers.py to safely extract these fields using .get() and fallback to self.instance values, preventing the unhandled KeyError.

How did you test this code?

Added a unit test (test_partially_updating_multivariate_option_success) in api/tests/unit/features/multivariate/test_unit_multivariate_views.py to verify that sending a PATCH request with only the key field correctly returns a 200 OK and preserves the existing default_percentage_allocation without throwing an error. Tested successfully via pytest.

@srijantrpth
srijantrpth requested a review from a team as a code owner August 15, 2026 16:43
@srijantrpth
srijantrpth requested review from khvn26 and a lite review from Copilot and removed request for a team August 15, 2026 16:43
@vercel

vercel Bot commented Aug 15, 2026

Copy link
Copy Markdown

@srijantrpth is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 15, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 1feaf577-f137-493f-9566-25e4974e35a9

📥 Commits

Reviewing files that changed from the base of the PR and between eb5081f and 34efb5b.

📒 Files selected for processing (1)
  • api/tests/unit/features/multivariate/test_unit_multivariate_views.py

Included review availability: Your plan includes up to 8 reviews per rolling hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The serializer now supports partial updates for multivariate feature options. It uses existing instance values when PATCH data omits the feature or allocation. Key uniqueness validation also uses the existing feature. A view test covers a partial key update and confirms that the percentage allocation remains unchanged.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 34efb

This change prevents partial multivariate-option updates from returning a 500 error while preserving existing values; no actionable merge-blocking risk remains after normal checks and review.

✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the api Issue related to the REST API label Aug 15, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: e1dd16f9-b08c-4af5-97d0-0f21c05b367f

📥 Commits

Reviewing files that changed from the base of the PR and between a013d7b and ad13fa6.

📒 Files selected for processing (2)
  • api/features/multivariate/serializers.py
  • api/tests/unit/features/multivariate/test_unit_multivariate_views.py

Comment thread api/features/multivariate/serializers.py Outdated
Comment thread api/tests/unit/features/multivariate/test_unit_multivariate_views.py Outdated
@coderabbitai

coderabbitai Bot commented Aug 15, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (2)
api/features/multivariate/serializers.py (1)

148-149: ⚠️ Potential issue | 🟠 Major

Resolve the existing key before applying this feature fallback.

When a PATCH changes feature but omits a non-null key, _validate_key_is_unique returns at Lines 145-147 before Lines 148-149 run. The existing key is not checked against the destination feature. This can violate unique_together = ("feature", "key") and produce a 500 instead of a serializer validation error. Resolve key from attrs or self.instance before the None check, and add a feature-only conflicting-key regression test.

This is based on the uniqueness contract in api/features/multivariate/models.py and the writable feature PATCH path in api/features/multivariate/views.py.

api/tests/unit/features/multivariate/test_unit_multivariate_views.py (1)

137-137: ⚠️ Potential issue | 🟡 Minor

Add the final newline.

Ruff 0.16.1 reports W292 at Line 137. Add a newline at the end of api/tests/unit/features/multivariate/test_unit_multivariate_views.py.

Source: Linters/SAST tools


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 360a512e-20b7-456e-b757-e96f8211284e

📥 Commits

Reviewing files that changed from the base of the PR and between a013d7b and dfa23f7.

📒 Files selected for processing (2)
  • api/features/multivariate/serializers.py
  • api/tests/unit/features/multivariate/test_unit_multivariate_views.py

@codecov

codecov Bot commented Aug 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.64%. Comparing base (a013d7b) to head (b3b8004).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8309      +/-   ##
==========================================
- Coverage   98.78%   98.64%   -0.14%     
==========================================
  Files        1604     1604              
  Lines       64335    64344       +9     
==========================================
- Hits        63551    63470      -81     
- Misses        784      874      +90     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@khvn26

khvn26 commented Aug 16, 2026

Copy link
Copy Markdown
Member

Hello @srijantrpth, thanks for your contribution.

Can you fix the remaining linting error?

flagsmith-lint-tests.....................................................Failed
- hook id: flagsmith-lint-tests
- exit code: 1

api/tests/unit/features/multivariate/test_unit_multivariate_views.py:111:1: FT003 Test name `test_partially_updating_multivariate_option_success` doesn't match `test_{subject}__{condition}__{expected}` (found 1 parts, expected 3)

@srijantrpth

srijantrpth commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Hi @khvn26 , Thanks for the review! I just pushed a commit to fix that linting error by renaming the test to match the required test_{subject}underscore underscore{condition}underscore underscore {expected} convention. Let me know if everything looks good now!

url = reverse(
"api-v1:projects:feature-mv-options-detail",
args=[project.id, multivariate_feature.id, mv_option.id],
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a compiled url here? See here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. See the url variable in test_list_mv_options__feature_in_other_project__returns_404 as an example.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @khvn26, I think the link you added for 'here' might be broken it just loops me back to this exact comment! Could you share the link to the example you meant to show me so I can get this updated?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it! Thanks for pointing me to that example. I've updated it to use the f-string URL pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issue related to the REST API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Partially updating a multivariate option returns 500

3 participants