ValidationError doesn't preserve JSON field's types #8785
Replies: 9 comments
-
I think the related code is here. Should or not handle the integer case before calling |
Beta Was this translation helpful? Give feedback.
-
I have a similar issue when raising an exception from a custom permissions class, where bools are not serialised to JSON correctly. I.e. this: [...]
raise PermissionDenied({"Error": True, "Access": False}) Results in {
"Error": "True",
"Access": "False"
} So I assume that the exception needs to handle bools as well before coercing into strings as @wjiuhe pointed out |
Beta Was this translation helpful? Give feedback.
-
Is there any repo mantainer here to make a PR? In the case is not, could I make a PR in my free time, or is expected to be forced to string always? |
Beta Was this translation helpful? Give feedback.
-
I too think it would be nice to preserve the primitive types of data sent back with the error information. However, I don't think it would be a good idea to change the default behavior. Consider this line in the official docs:
Emphasis on the word "textual". Also, this behavior is baked into so many existing applications already. Changing it would break a lot of stuff. Maybe a special parameter could be added to the exception classes, like raise PermissionDenied({"Error": True, "Access": False}) results in {
"Error": "True",
"Access": "False"
} but raise PermissionDenied({"Error": True, "Access": False}, json_parse_detail=True) results in {
"Error": true,
"Access": false
} |
Beta Was this translation helpful? Give feedback.
-
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Beta Was this translation helpful? Give feedback.
-
Hello, can you please atleast contribute the failing test case for this issue? |
Beta Was this translation helpful? Give feedback.
-
This looks like the correct take to me. |
Beta Was this translation helpful? Give feedback.
-
Yep, an optional parameter as @ChrisCrossCrash proposed would be nice! |
Beta Was this translation helpful? Give feedback.
-
I just opened an discussion for this problem and then found this Issue.. welp. I agree, an optional parameter would be really useful in this case! |
Beta Was this translation helpful? Give feedback.
-
Checklist
master
branch of Django REST framework.Steps to reproduce
I have a Response object with some Enums which I use in the frontend to identify some specific issues. I'm returning that response raising a ValidationError:
The problem is that, in this case, the fields
code
andinternal_code
are integer, but when they are returned they're implicitly casted to string for some reason:I have some views which has the same response but they're returned using JsonResponse in
views.py
:JsonResponse({...}, safe=False)
The result:
And this works correctly, so I think it's a problem with the serialization applied in the ValidationError returning.
Expected behavior
Preserve the primitives types of information in the returned JSON object as shown in the above img (with JsonResponse)
Beta Was this translation helpful? Give feedback.
All reactions