fix(api): handle null environment in validate_environment#6937
fix(api): handle null environment in validate_environment#6937wavebyrd wants to merge 5 commits intoFlagsmith:mainfrom
Conversation
|
Someone is attempting to deploy a commit to the Flagsmith Team on Vercel. A member of the Team first needs to authorize it. |
|
Thanks for the feedback @emyller — you're right. Updated to raise a |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
api/features/serializers.py
Outdated
|
|
||
| def validate_environment(self, environment): # type: ignore[no-untyped-def] | ||
| if environment is None: | ||
| raise serializers.ValidationError("This field may not be null.") |
There was a problem hiding this comment.
ValidationError prevents intended context-based environment fallback
High Severity
Raising ValidationError in validate_environment when environment is None prevents the object-level validate() from ever running. In DRF, field-level validation errors block execution of validate(). The validate() method at line 609 has an intentional fallback — attrs.get("environment") or self.context["environment"] — designed to resolve environment from the serializer context when it's absent from request data. Since FeatureState.environment is null=True, DRF passes None through to validate_environment. Returning None here instead of raising would let the fallback work as intended.
Additional Locations (1)
702d558 to
bd4b8b3
Compare
|
You have used all of your free Bugbot PR reviews. To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial. |
|
Moved the null check into validate() so the context fallback still works. If environment is None after checking both the payload and context, it now raises a ValidationError there instead. |
de112d6 to
d9bf73d
Compare
|
Hi @wavebyrd , can you check this PR please - it looks like you've perhaps made a mistake in a rebase or merge because it's showing 3000+ files changed. |
…6597) When a request passes a null environment value to the featurestates endpoint, `validate_environment` raises an `AttributeError` because it tries to access `.id` on `None`. Guard against `None` and return early, letting the broader `validate` method resolve the environment from the serialiser context.
Address review feedback: raise ValidationError instead of passing null through, since the nullable environment column is a historical data layer artefact and not intended API behaviour.
for more information, see https://pre-commit.ci
Field-level validate_environment was blocking the context fallback in validate() from ever running. Now validate_environment just guards the .id access, and validate() resolves environment from context before rejecting null.
9576e83 to
02253cf
Compare
|
Fixed the rebase - should now show only the 4 commits for this fix. Sorry about that! |
for more information, see https://pre-commit.ci


Summary
Fixes #6597
FeatureStateSerializerBasic.validate_environmentraises anAttributeErrorwhen a request passesnullfor the environment field. The method tries to access.idon the environment parameter without first checking forNone.Since
null=Trueon the environment column is a historical data layer detail and not intended API behaviour, this rejects null with aValidationErrorinstead of passing it through.Changes
api/features/serializers.py: AddedNonecheck invalidate_environmentthat raisesValidationErrorbefore accessingenvironment.idapi/tests/unit/features/test_unit_features_serializers.py: Added test verifying null environment is rejected with a validation errorTest plan
test_feature_state_serializer_basic__null_environment__returns_validation_errorverifies passingenvironment: Noneis rejected and does not raiseAttributeError