Is it possible to use drf-standardized-errors
with drf-spectacular
to document the validation_error
type errors generated by an APIView
that manually uses a serialiser to validate GET parameters?
I have a basic APIView
that accepts a series of GET parameters and returns a single value – the result of a calculation based on the provided parameters. I manually use a serialiser to validate the parameters prior to using them, thus the view can return a standardised 400 response containing validation_error
type errors. However, this type of error response is not documented by drf-spectacular
for this view. Other views, defined using ViewSet
and declared serialisers, have their 400 responses documented correctly.
APIView
obviously does not support declaring a serialiser, so I use the following to have both the parameters and the view’s 200 response documented according to the definition of the relevant serialisers:
@extend_schema(
parameters=[
ParameterSerializer
],
response={
status.HTTP_200_OK: ResultSerializer
}
)
class PriceLookupView(views.APIView):
def get(self, request):
...
This generates the documentation as I would expect, apart from the 400 response, which only contains the client_error
type.
The above does generate a warning about not being able to guess a serialiser. So, following its suggestion, I have also tried using GenericAPIView
and declaring serializer_class
, despite it not being utilised by DRF itself:
class PriceLookupView(generics.GenericAPIView):
serializer_class = ParameterSerializer
This does not alter the output of the generated documentation. In my experimentation, the serialiser declared on the class in this approach seems to control the documentation of the view’s 200 response only.
I haven’t been able to find any info on this use case – which makes me think I’m either not using the right keywords, or the solution is so obvious it hasn’t been asked before and I’m just missing something simple. Is this possible?