Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33130 closed Bug (fixed)

ErrorDict doesn't pass the dict type check in JsonResponse.

Reported by: Jaap Roes Owned by: Jaap Roes
Component: Forms Version: 4.0
Severity: Release blocker Keywords:
Cc: David Smith Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Just ran the test suite of a project on Django 4.0a1 (with Python 3.8) and ran into the following issue:

We have a fairly simple FormView that defines the following form_invalid method:

def form_invalid(self, form):
    return JsonResponse(form.errors, status=400)

This worked fine before, but with Django 4 this raises the following error:

TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False.

Wrapping form.errors with dict() fixes this, but I'm not sure this should be necessary.

Change History (10)

comment:1 by Mariusz Felisiak, 3 years ago

Cc: David Smith added
Severity: NormalRelease blocker
Summary: ErrorDict doesn't pass the dict type check in JsonResponseErrorDict doesn't pass the dict type check in JsonResponse.
Triage Stage: UnreviewedAccepted

Thanks for the report! Form.errors should be recognizable as a dict according to docs.

Regression in 456466d932830b096d39806e291fe23ec5ed38d5.

comment:2 by Mariusz Felisiak, 3 years ago

It should be enough to subclass dict:

class ErrorDict(UserDict, dict, RenderableErrorMixin):

comment:3 by Jaap Roes, 3 years ago

Owner: changed from nobody to Jaap Roes
Status: newassigned

comment:4 by Jaap Roes, 3 years ago

Has patch: set

PR (There are two mariadb failures, but those seem unrelated)

comment:5 by Mariusz Felisiak, 3 years ago

Needs tests: set
Patch needs improvement: set

comment:6 by Jaap Roes, 3 years ago

Sadly the fix isn't as simple as initially thought (comment:2). It solves the issue with the type check in JsonResponse but the actual json serialization results in an empty JSON object instead of the intended result of a JSON object with field names and error messages.

I will write additional tests to also check if the ErrorDict also serializes in the desired way, and come up with a better fix (the PR comments already have a good indication to what that may be).

comment:7 by Jaap Roes, 3 years ago

Needs tests: unset
Patch needs improvement: unset

I've updated the PR with a JSON serialization tests and a modified fix so make the JSON serialization test pass.

comment:8 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 7fe9b6f:

Fixed #33130 -- Restored form errors to be a dict.

Regression in 456466d932830b096d39806e291fe23ec5ed38d5.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 25cfa5db:

[4.0.x] Fixed #33130 -- Restored form errors to be a dict.

Regression in 456466d932830b096d39806e291fe23ec5ed38d5.

Backport of 7fe9b6f6df16fa875fe360a1c7d0ac53fcf08a53 from main

Note: See TracTickets for help on using tickets.
Back to Top