-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Allow option for conflicts in validation to result in 409
, not 400
.
#1848
Comments
Yup, this. With 3.0 the handling of response style for validation errors will be down to the exception handler. I'm not clear on how you'd best deal with differentiating conflicts from other validation errors within the serializer API, but it'd certainly be easier that overriding the view code. Does that sound sufficient to resolve this from your point of view? |
IntegrityError
should return 409
, not 400
409
, not 400
.
409
, not 400
.409
, not 400
.
Our office has some dedicated hack days coming up; let me know if there's an area you'd like us to look at to help contribute to a solution. |
If it's built in to the serializer API, this is how I'd imagine doing it: Either set a global config, so As far as I can tell, users that want a 409 returned want to do so on all DB conflicts (in Django, this means getting an Thoughts? |
(My preference is for a global config; that suits our use case.) |
I'd rather have it handled by either the renderers and the exception handler. |
Suggest further discussion of this once we've got some release notes for 3.0, as the validation changes there impact (and to my mind, resolve) this issue. |
@tomchristie I've got about 16 hours I can commit to helping with this issue (or possible something else DRF-related) next Thurs-Fri. Do let me know how I can help. |
Ok, thanks to @tomchristie I've tracked down where exactly this issue comes from: Effectively, by default Django sets
|
Ok, so the easy solution is to pass
|
Also noting that it should be possible to update the generic view classes to automatically do this; with the setting defaulting to the existing behavior, existing users will have to do nothing and everything will work as expected. Those that want a |
This isn't quite what I'm expecting as the end result if this. It's enough for me that you're able to customize your serializers and/or exception handler in order to cater for this use case. Personally I'm still if the opinion that choosing either 400 or 409 for all validation errors is perfectly adequate. Behaviourally that's enough info for the client. A validation error is just that - irrelevant if it was caused due to a DB constraint or a code constraint. |
If I understand correctly, DRF3 doesn't even call
We may still not be able I use I'd still prefer a mechanism to handle this automatically with generic views, but I see that a) DRF is never going to do that, and b) my boss is never going to accept returning |
It does, yes. The uniqueness validation in 3.0 is enforce by validators, rather than using full_clean. Firstly, braindump of various possible approaches...
How I would prob tackle this if I really wanted to be able to use standard ModelSerializers and the default generic views.
Little bit of work there, but end result would be a serializer that you could use exactly like the existing |
@tomchristie What ever came of your braindump in the most recent comment? It looks like 400s are raised right now with no easy way of changing that out. Further I think some of the issues you raise about DB errors are still present in #3876. |
No further work here to discuss. Somewhat related to #3775. |
how to i fix it |
didn't #4550 fix it? |
I know this was brought up before, but I have to disagree with the rationale that otherwise valid input that fails because of a unique constraint should return a
400 Bad Request
.The RFC cited in the above thread notes that
400
should be reserved for malformed syntax; i.e. keys that aren't used or values that aren't the correct format. A409
suggests a conflict, i.e. though an email address may be properly formatted, it conflicts with an existing entry in the database because that particular field is required to be unique. While agree with the idea that attempting toPOST
to create an entity with conflicting data is a "bad request",409
is a more specific error code to return for a conflict to to uniqueness constraints.Regardless of that issue, however, is the fact that
409
is a de facto standard response when otherwise valid input conflicts with uniqueness constraints. While we could simple override every HTTP method on every view class that would require us to return409
s instead of generic400
s for uniqueness checks, we would also have to override our serializers, resulting in more and more complex code when in all other respects we would be able to use the existing generic API views with little to no modifications. The other solution—maintaining our own fork of DRF—is even less palatable.If some DRF users prefer the
400
only responses for data validation, perhaps a solution to to offer a way to set whether400
s or409
s are returned for conflicts.The text was updated successfully, but these errors were encountered: