accurate-validation-error-response-with-drf-spectacular #5
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.
The goal of this PR: describe validation error response accurately. That means API consumers should be able to know which error codes are returned for each field when checking the API schema.
Here's how validation errors look in swagger UI:
validation_error_example
validation_error_schema
Usage
If you're not overridding the postprocessing hook setting from drf-spectacular, set it to
But if you're already overriding it, make sure to replace the enums postprocessing hook from drf-spectacular with the one from this package. The hook will avoid raising warnings for dynamically created error code enums per field.
Currently, that is taken care of while accounting for nested and list serializers, composite serializer fields (like ListField and DictField) as well as validation errors raised by the django filter backend.Custom error codes are also supported as long as you follow the DRF way of doing that. That means adding the error code as key in the serializerdefault_error_messages
(or directly toself.error_messages
) and then you can callself.fail
to raise the validation error:However, using this with drf-spectacular can result in many warnings like these:After raising the above issue in drf-spectacular, the recommendation was to simply deal with this special use case, so I went ahead and created a postprocessing hook that excludes error component enums from being processed (the same hook with a minor change in which enums are collected). Package users will be encouraged to use this hook instead of the one provided by drf-spectacular.One more item to think about: Ability to add custom error codes to specific fields. Currently, if the package user defines a custom error the "drf way" i.e. add it toField.default_error_messages
orField.error_messages
(same for forms with django-filters), the error code will show up automatically in the error response schema. Should another way be added to easily add error codes at theAutoSchema
class level?That's because the list of errors relative to each serializer is represented by a PolymorphicProxySerializer which allows specifying multiple serializers and each of them describes the error response for one field. A sample serializer would be like this:The field-specific serializers are generated dynamically based on the operation. So, it is very likely that the "attr" or "code" choices are the same for different operations in the API. When that happens, the postprocessing hook from drf-spectacular sees that and raises the warnings above.As for solutions, current options are:disable the postprocessing hook from drf-spectacular: means losing its benefitscreate a new postprocessing hook that calls the drf-spectacular hook after wrapping it inside aGENERATOR_STATS.silence()
: risks losing the good-to-hear-about warningsreach out to drf-spectacular maintainer to see if they're open to adding a feature to drf-spectacular that fixes this issue: maybe disable generating enums for certain fields, ...