Skip to content

mv-options PATCH raises KeyError: 'feature' (500) on partial update #8073

Description

@sentry

Sentry: FLAGSMITH-API-69Q (https://flagsmith.sentry.io/issues/7627331389/)

What happens

A PATCH to /api/v1/projects/{project_pk}/features/{feature_pk}/mv-options/{pk}/ that omits feature from the body raises an unhandled KeyError: 'feature' → HTTP 500.

Root cause

MultivariateFeatureOptionSerializer.validate() (api/features/multivariate/serializers.py:83) reads attrs["feature"]:

def validate(self, attrs):
    attrs = super().validate(attrs)
    total_sibling_percentage_allocation = (
        self._get_siblings(attrs["feature"]).aggregate(...)
    )

On partial_update, feature is only present in attrs if the client sent it, so any partial PATCH that omits it (e.g. updating only default_percentage_allocation, type, or key) 500s.

Reproduce

PATCH /api/v1/projects/<p>/features/<f>/mv-options/<o>/
{ "default_percentage_allocation": 40 }
→ 500, KeyError: 'feature'

Including "feature": <f> in the body avoids it.

Suggested fix

Fall back to the instance on partial updates:

feature = attrs.get("feature") or getattr(self.instance, "feature", None)

Notes

Surfaced via the Flagsmith CLI (feature variant update sends partial PATCHes). Worked around CLI-side by always sending feature; other clients doing partial PATCHes still hit this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions