Skip to content

Allow option for conflicts in validation to result in 409, not 400. #1848

@foresmac

Description

@foresmac

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. A 409 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 to POST 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 return 409s instead of generic 400s 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 whether 400s or 409s are returned for conflicts.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions