Open jamescooke opened 4 years ago
This has to do with how we append the MaxValueValidator
to the end of the list of validators if the max_value
argument is passed in.
This is especially unintuitive because we strip out the existing MaxValueValidator
when we map the model field to the serializer field.
I am not aware of the consequences of changing the location of where that validator is injected into the list of validators.
BTW I found this bug using https://github.com/kiwicom/schemathesis - definitely worth checking out if you're building any APIs that are described by Swagger / OpenAPI :+1:
My gut response.. I'm inclined to change how we handle uniqueness checks, and somewhat follow Django's lead. Looking at full_clean
, fields are validated first and date/uniqueness checks later. I think one of the important distinctions here is that field validation is typically Python-only, while date/uniqueness checks execute ORM calls.
I'm thinking we could leave the UniqueValidator
as-is so we don't break usage for existing declarative cases, but the ModelSerializer
could be changed to instead add a uniqueness validator to the serializer's Meta.validators
, similar to how we handle unique together. So, the generated ModelSerializer would go from something like:
class MySerializer(ModelSerializer):
my_field = CharField(validators=[UniqueValidator(queryset=MyModel.objects.all())])
to
class MySerializer(ModelSerializer):
class Meta:
validators = [
UniqueFieldValidator(queryset=MyModel.objects.all(), field_name='my_field'),
]
By moving the uniqueness check to the serializer from the field, this would ensure that the max value validator is checked first. And in general, I guess a general guideline should be field validators shouldn't touch the DB - that should ideally be reserved for serializer validators.
This may also just be an issue with the sqlite backend. https://code.djangoproject.com/ticket/27397
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Checklist
master
branch of Django REST framework.Steps to reproduce
First: check Django behaviour
Given a simple Model with a single
IntegerField
, when working with SQLite (I'm running version 3.27 which is officially supported by Django) it is possible to generate an overflow error.Model:
Test:
Gives an
OverflowException
:However, using
MaxValueValidator
allows us to protect SQLite from this overflow. So if we change the model and test, then Django raises aValidationError
complaining thatvalue
is too large:Model:
Test:
In this way we can use Django to protect SQLite from the overflow - all is good at the Django level.
DRF behaviour
Now we make a
ModelSerializer
for this updated model with theMaxValueValidator
in place and test it with the large int:Expected behaviour
This test should pass. The DRF model serializer should raise a
ValidationError
that complains thatvalue
is too large, something like:Actual behaviour
However the test fails with an
OverflowError
which arises from the uniqueness check:Thoughts
(Ignore as appropriate)
It looks to me like the check for uniqueness happens before the field level validation that happens as a result of the
MaxValueValidator
because when I dump the field'svalidators
I get:This means that round-tripping the database happens before checking the max value. This is unexpected behaviour and I was surprised when I found it - I would expect as much validation as possible to take place before the database hit.
I also experimented with adding a field-level
validate_value()
function to the serializer, but it appears thatrun_validators()
happens before that is trigged, so it's no help in preventing the overflow.