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 Dan Palmer)

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 Tim Graham, 6 years ago

Type: UncategorizedCleanup/optimization

Does using a custom encoder with test client's json_encoder parameter solve your issue?

comment:2 by Dan Palmer, 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

Version 0, edited 6 years ago by Dan Palmer (next)

comment:3 by Adam Johnson, 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 Dan Palmer, 6 years ago

Great, I'll update the ticket/PR for that and document it as a 2.2 feature.

comment:5 by Dan Palmer, 6 years ago

Description: modified (diff)
Summary: Support serialising JSON arrays in Django Test ClientSupport serialising JSON types other than objects in Django Test Client

comment:6 by Tim Graham, 6 years ago

Has patch: set
Patch needs improvement: set
Summary: Support serialising JSON types other than objects in Django Test ClientMake the test client serialize JSON types other than dict
Triage Stage: UnreviewedAccepted

comment:7 by Dan Palmer, 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:8 by Dan Palmer, 6 years ago

PR

Updated with full support, modulo the issue mentioned above.

comment:9 by Tim Graham, 6 years ago

Patch needs improvement: unset
Summary: Make the test client serialize JSON types other than dictMake the test client serialize list/tuple for JSON mime type
Triage Stage: AcceptedReady 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.

comment:10 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: newclosed

In e1816669:

Fixed #29687 -- Allowed the test client to serialize list/tuple as JSON.

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