Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Error Pointers for APIViews#1044

Unanswered
exiareinert-hpa asked this question inQ&A
Jan 7, 2022· 1 comments· 2 replies
Discussion options

Hello!

I have an API view which inherits fromrest_framework.views.APIView, rather thanGenericAPIView. Before (I believe) PR#986 was merged, formatting for required field errors used field names, e.g.:

{    "errors": [        {            "detail": "This field is required.",            "status": "400",            "source": {                "pointer": "/data/attributes/address"            },            "code": "required"        }    ]}

Because pointers are now set only if the view inherits fromGenericAPIView, required field errors are now much less informative:

{    "errors": [        {            "detail": "This field is required.",            "status": "400",            "code": "required"        }    ]}

It doesn't make sense for my API view to inherit fromGenericAPIView, because it provides the result of a calculation rather than directly returning model data. Would it be possible to reinstate the previous behavior in cases like this?

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

Thanks for your feedback.

Before the fix of#986 the error pointer base was hard coded and was only correct in some cases and in other it was not. And the only way which seemed plausible is to make calculation based on theGenericAPIView as this is the lowest view class which enforces setting of serializer class. And the serializer class is needed to determine the correct pointer.

This said we might change this check but for this it is important to know the different APIView use cases.

So could you paste yourAPIView here to see how you have configured it? Thanks.

You must be logged in to vote
2 replies
@exiareinert-hpa
Comment options

The API view (code example below) does use a serializer. Perhaps an alternate solution could involve manually declaring a serializer attribute?

class CalculationView(APIView):    permission_classes = (IsAuthenticated,)    @swagger_auto_schema(        query_serializer=CalculationSerializer,        responses=custom_view_to_response("get", "CalculationView"),    )    def get(self, request, format=None):        param_serializer = CalculationSerializer(data=request.GET)        param_serializer.is_valid(raise_exception=True)        calculator = Calculate(param_serializer.data)        calculator.init()        data = calculator.call()        return Response(data)
@sliverc
Comment options

Thanks for the example. As you do not setresource_name in your view I assume that the data returned bycalculator.call() is already in JSON:API spec format. Is this correct?

I think in such a case where theAPIView is manually building the result it should also manually format the errors. For this you can overwritehandle_exception of your view and use the utilityrest_framework_json_api.utils.format_error_object with a fix pointer to attributes when the exception is a singleValidationError or otherwise call super. I haven't tested this but should work in theory.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@exiareinert-hpa@sliverc

[8]ページ先頭

©2009-2025 Movatter.jp