Skip to content

fix(Features): Allow partial updates of segment overrides via the feature states API - #9123

Open
Pranav-Singh-Devloper wants to merge 1 commit into
Flagsmith:mainfrom
Pranav-Singh-Devloper:fix/9118-segment-override-partial-update
Open

Pranav-Singh-Devloper wants to merge 1 commit into
Flagsmith:mainfrom
Pranav-Singh-Devloper:fix/9118-segment-override-partial-update

Conversation

@Pranav-Singh-Devloper

Copy link
Copy Markdown

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 #9118

A partial update (PATCH) of a segment override through /api/v1/features/featurestates/{id}/ returned HTTP 500 with KeyError: 'environment'. FeatureStateSerializerBasic.validate() falls back to self.context["environment"] when the request body has no environment, and SimpleFeatureStateViewSet doesn't put an environment in the context.

validate() now falls back to the feature state's own environment on updates. If there is no environment at all (in the body, the context or the instance), it returns a validation error on environment instead of a 500.

No docs change: this restores the documented behaviour of PATCH.

How did you test this code?

  • New view test test_partial_update_segment_override__without_environment__updates_feature_state: PATCH {"enabled": ...} with MANAGE_SEGMENT_OVERRIDES now returns 200 and updates the override. Without the fix it fails with KeyError: 'environment'.
  • New serializer test test_feature_state_serializer_basic__no_environment_anywhere__returns_validation_error covers the new error branch.
  • make test opts='tests/unit/features/test_unit_features_views.py tests/unit/features/test_unit_features_serializers.py -k "segment_override or without_environment or no_environment or featurestate or feature_state_serializer"': 33 passed.
  • ruff, ruff format, flagsmith-lint-tests and mypy pass on the changed files.
  • Manually, against a self-hosted 2.295.2 with the same change applied: the PATCH from the issue returned 200 and updated the override.

…ture states API

A PATCH to /api/v1/features/featurestates/{id}/ without an environment raised
KeyError: 'environment' (HTTP 500), because FeatureStateSerializerBasic.validate
read it from the serializer context, which this view does not set. Fall back to
the feature state's own environment, and return a validation error instead of
a 500 when no environment is available at all.

Closes Flagsmith#9118
@Pranav-Singh-Devloper
Pranav-Singh-Devloper requested a review from a team as a code owner November 26, 2026 13:01
@Pranav-Singh-Devloper
Pranav-Singh-Devloper requested review from khvn26 and removed request for a team November 26, 2026 13:01
@vercel

vercel Bot commented Nov 26, 2026

Copy link
Copy Markdown

@Pranav-Singh-Devloper is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

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

@cubic-dev-ai cubic-dev-ai 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.

No issues found across 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Auto-approval skipped because this PR is from an external contributor.

Re-trigger cubic

This branch has not been deployed

No deployments
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.

PATCH on a segment override feature state returns 500 (KeyError: 'environment')

1 participant