Opened 6 years ago
Closed 6 years ago
#29687 closed Cleanup/optimization (fixed)
Make the test client serialize list/tuple for JSON mime type
Reported by: | Dan Palmer | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | 2.1 |
Severity: | Normal | Keywords: | |
Cc: | 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 (last modified by )
This is a follow up to #29082, which added support for serialising JSON objects in the Django Test Client.
Currently JSON types other than objects are not supported.
This is my first ticket (I think), so any advice on what's required would be appreciated. I'm planning to open a PR on GitHub for this.
Change History (10)
comment:1 by , 6 years ago
Type: | Uncategorized → Cleanup/optimization |
---|
comment:2 by , 6 years ago
It doesn't - the bug is in the detection in the test client of whether to serialise with the encoder or not. I've fixed it in https://github.com/django/django/pull/10313
comment:3 by , 6 years ago
This isn't a bug on 2.1, but the docs could be marginally clearer by saying it *only* happens when data
is a dictionary.
Adding support for detecting arrays would be a 2.2 feature. But then it should probably detect all other types that json.dumps
can do too - booleans, integers, floats, and strings? This could break more workflows, especially subtly as json.dumps
on a string will normally just yield extra quote marks.
comment:4 by , 6 years ago
Great, I'll update the ticket/PR for that and document it as a 2.2 feature.
comment:5 by , 6 years ago
Description: | modified (diff) |
---|---|
Summary: | Support serialising JSON arrays in Django Test Client → Support serialising JSON types other than objects in Django Test Client |
comment:6 by , 6 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
Summary: | Support serialising JSON types other than objects in Django Test Client → Make the test client serialize JSON types other than dict |
Triage Stage: | Unreviewed → Accepted |
comment:7 by , 6 years ago
So, while implementing I've found a minor edge case.
From the Python docs table of Python to JSON type conversions list:
Python | JSON |
---|---|
int, float, int- & float-derived Enums | number |
Unfortunately I can't figure out how to test a float-derived enum. I don't use Python Enums much, so I might just be getting this wrong, but looking at the source for the standard JSONEncoder, I can't see any mention of support for this feature. It explicitly calls out IntEnums
, but nothing float related for them.
For these reasons, I'm going to just leave this.
comment:9 by , 6 years ago
Patch needs improvement: | unset |
---|---|
Summary: | Make the test client serialize JSON types other than dict → Make the test client serialize list/tuple for JSON mime type |
Triage Stage: | Accepted → Ready for checkin |
Further discussion on the pull request decided to only support list and tuple absent use cases for other types and the possibility of over-broad exception silencing if trying to serialize data unconditionally.
Does using a custom encoder with test client's
json_encoder
parameter solve your issue?