Skip to content

fix(features): allow partial feature state updates without the environment - #9145

Open
anshuraj669 wants to merge 1 commit into
Flagsmith:mainfrom
anshuraj669:fix/feature-state-partial-update-environment
Open

anshuraj669 wants to merge 1 commit into
Flagsmith:mainfrom
anshuraj669:fix/feature-state-partial-update-environment

Conversation

@anshuraj669

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. (Not required: bug fix, no behaviour change for valid requests.)
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

Closes #9144

A partial update of a feature state that leaves out environment returns a 500 instead of updating the feature state:

PATCH /api/v1/features/featurestates/{id}/
{"enabled": true}

-> 500, KeyError: 'environment' at features/serializers.py, FeatureStateSerializerBasic.validate()

SimpleFeatureStateViewSet does not put the environment in the serializer context, and validate() read it from self.context["environment"] whenever the body did not include it. A PATCH that only sends enabled (for example, a script or integration turning a flag off with a master API key) therefore always failed. Sending environment and feature along was the only workaround.

validate() now uses, in order: the environment in the request, the one in the serializer context, and finally the environment of the feature state being updated. Requests that already send the environment behave as before, and changing a feature state's environment is still rejected by validate_environment().

I found this while building an integration that turns a flag off through the Admin API with a master API key.

How did you test this code?

  • Added test_partial_update_feature_state__environment_omitted__updates_feature_state. It runs with both a user token and a master API key (admin_client_new). Without the fix it fails with the same KeyError: 'environment'; with the fix it passes and the feature state is updated.
  • pytest tests/unit/features tests/unit/environments: 1334 passed, 5 skipped.
  • ruff check, ruff format --check and mypy on the changed files: clean.
  • Manually, against a self-hosted 2.295.2 instance: PATCH /api/v1/features/featurestates/{id}/ with {"enabled": false} and an Api-Key header returned a 500 before the fix.

🤖 Generated with Claude Code

…nment

PATCH /api/v1/features/featurestates/{id}/ with a body such as
{"enabled": true} returned a 500 (KeyError: 'environment'), for both user
tokens and master API keys. SimpleFeatureStateViewSet does not put the
environment in the serializer context, and the serializer's validate()
read it from there when the request body omitted it. A partial update now
falls back to the feature state's own environment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anshuraj669
anshuraj669 requested a review from a team as a code owner November 27, 2026 12:53
@anshuraj669
anshuraj669 requested review from khvn26 and removed request for a team November 27, 2026 12:53
@vercel

vercel Bot commented Nov 27, 2026

Copy link
Copy Markdown

@anshuraj669 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 27, 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 2 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 /api/v1/features/featurestates/{id}/ without environment returns 500 (KeyError: 'environment')

1 participant