-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix validation for ListSerializer when many=True #9774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
p-r-a-v-i-n
wants to merge
4
commits into
encode:main
Choose a base branch
from
p-r-a-v-i-n:fix/bulk-validation-serializer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−1
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
from rest_framework import serializers | ||
from rest_framework.exceptions import ErrorDetail | ||
from tests.models import ( | ||
CustomManagerModel, NullableOneToOneSource, OneToOneTarget | ||
CustomManagerModel, EmailPKModel, ListModelForTest, NullableOneToOneSource, | ||
OneToOneTarget | ||
) | ||
|
||
|
||
|
@@ -775,3 +776,67 @@ def test(self): | |
queryset = NullableOneToOneSource.objects.all() | ||
serializer = self.serializer(queryset, many=True) | ||
assert serializer.data | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestManyTrueValidationCheck: | ||
""" | ||
Tests ListSerializer validation with many=True across different primary key types | ||
(integer and email). | ||
""" | ||
|
||
def setup_method(self): | ||
self.obj1 = ListModelForTest.objects.create(name="valid", status="new") | ||
self.obj2 = ListModelForTest.objects.create(name="invalid", status="") | ||
self.email_obj1 = EmailPKModel.objects.create(email="[email protected]", name="A") | ||
self.email_obj2 = EmailPKModel.objects.create(email="[email protected]", name="B") | ||
|
||
self.serializer, self.email_serializer = self.get_serializers() | ||
|
||
def get_serializers(self): | ||
class ListModelForTestSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = ListModelForTest | ||
fields = ("id", "name", "status") | ||
|
||
def validate_status(self, value): | ||
if value and not self.instance.is_valid: | ||
return False | ||
return value | ||
|
||
class EmailPKSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = EmailPKModel | ||
fields = ("email", "name") | ||
read_only_fields = ('email',) | ||
|
||
def validate_name(self, value): | ||
if value and not self.instance.is_valid: | ||
return False | ||
return value | ||
|
||
return ListModelForTestSerializer, EmailPKSerializer | ||
|
||
def test_run_child_validation_with_many_true(self): | ||
input_data = [ | ||
{"id": self.obj1.pk, "name": "other", "status": "new"}, | ||
{"id": self.obj2.pk, "name": "valid", "status": "progress"}, | ||
] | ||
|
||
serializer = self.serializer([self.obj1, self.obj2], data=input_data, many=True) | ||
assert serializer.is_valid(), serializer.errors | ||
|
||
serializer = self.serializer(ListModelForTest.objects.all(), data=input_data, many=True) | ||
assert serializer.is_valid(), serializer.errors | ||
|
||
def test_validation_error_for_invalid_data(self): | ||
input_data = [{"id": self.obj1.pk, "name": "", "status": "mystatus"}] | ||
|
||
serializer = self.serializer([self.obj1], data=input_data, many=True) | ||
assert not serializer.is_valid() | ||
assert "name" in serializer.errors[0] | ||
|
||
def test_email_pk_instance_validation(self): | ||
input_data = [{"email": "[email protected]", "name": "bar"}] | ||
serializer = self.email_serializer(instance=EmailPKModel.objects.all(), data=input_data, many=True) | ||
assert serializer.is_valid(), serializer.errors |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see plenty of ways to break this: what if the PK is a UUID field called
id
but serialized asuuid
? Sometimes it's calleduid
... Are we going to handle all possible field name people are using in the wild?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s true, but the same is already true for single updates. If the PK is serialized under a different name (e.g. uuid, uid, etc.), DRF can’t resolve it automatically there either unless the serializer is customized.
If anything works for single updates, it will also work for bulk updates, the constraints are the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree, with a single instance, you have the instance and the data, so it's a 1 to 1 mapping and you know it should match.
With a list of dicts on one hand and a list of instances/queryset on the other, you need to map which dict corresponds to which instance.
This mapping will depend on the use case, and needs a unique identifier somewhere (which could be anything: PK, email, slug, combination of fields...). Hence why users need to do it, DRF can't do it for them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point about single updates having a direct instance - data mapping. But I’d still argue the difference is one of quantity rather than fundamentals.
Even in single updates, DRF assumes that the mapping is correct only because the caller provided the right instance. If the serializer is misaligned (e.g. PK serialized under another field, or a different uniqueness condition like email/slug), DRF doesn’t solve that, the user has to customize the serializer.
For bulk updates, the requirement is the same: there needs to be some unique identifier to match instance ↔ data. Whether that identifier is pk, uuid, email, or something else, the logic isn’t different from single updates, just applied across a list.
So I don’t see it as “DRF can’t do it at all,” but more that DRF could apply the same assumptions it already makes in the single case, and users who need different identifiers would still override/customize.