fix: add null validation for environment in FeatureStateSerializerBasic#7099
fix: add null validation for environment in FeatureStateSerializerBasic#7099Pwongchaiya wants to merge 2 commits intoFlagsmith:mainfrom
Conversation
|
@Pwongchaiya is attempting to deploy a commit to the Flagsmith Team on Vercel. A member of the Team first needs to authorize it. |
for more information, see https://pre-commit.ci
Codecov Report❌ Patch coverage is
❌ Your patch check has failed because the patch coverage (50.00%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #7099 +/- ##
==========================================
- Coverage 98.31% 98.26% -0.06%
==========================================
Files 1335 1335
Lines 49760 49818 +58
==========================================
+ Hits 48923 48952 +29
- Misses 837 866 +29 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Thanks for the fix — this is a real bug and worth addressing! A couple of suggestions:
Consider overriding the field instead of adding a validator check
The environment FK on the model has null=True, so DRF auto-generates the serializer field with allow_null=True. Since this serializer always expects a real environment (the validate() method falls back to self.context["environment"]), you could disallow null at the field level instead:
class FeatureStateSerializerBasic(WritableNestedModelSerializer):
environment = serializers.PrimaryKeyRelatedField(
queryset=Environment.objects.all(),
allow_null=False,
)
This way DRF handles the null rejection automatically with a standard error message, and validate_environment only needs to deal with the "cannot change environment" logic.
Tests needed
The project requires 100% diff coverage for backend PRs, so this will need at least one test — e.g. sending "environment": null and asserting a 400 response.
Summary
FeatureStateSerializerBasic.validate_environmentto raise aValidationErrorwhen the environment isNone, preventing a 500 error response.Test plan
🤖 Generated with Claude Code