[Django] #29687: Support serialising JSON arrays in Django Test Client

49 views
Skip to first unread message

Django

unread,
Aug 18, 2018, 7:56:50 AM8/18/18
to django-...@googlegroups.com
#29687: Support serialising JSON arrays in Django Test Client
---------------------------------------------+------------------------
Reporter: danpalmer | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
This is a follow up to #29082, which added support for serialising JSON
objects in the Django Test Client.

Currently JSON arrays 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29687>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 18, 2018, 8:18:07 AM8/18/18
to django-...@googlegroups.com
#29687: Support serialising JSON arrays in Django Test Client
-------------------------------------+-------------------------------------
Reporter: danpalmer | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* type: Uncategorized => Cleanup/optimization


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:1>

Django

unread,
Aug 18, 2018, 8:23:13 AM8/18/18
to django-...@googlegroups.com
#29687: Support serialising JSON arrays in Django Test Client
-------------------------------------+-------------------------------------
Reporter: danpalmer | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by danpalmer):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:2>

Django

unread,
Aug 18, 2018, 9:30:31 AM8/18/18
to django-...@googlegroups.com
#29687: Support serialising JSON arrays in Django Test Client
-------------------------------------+-------------------------------------
Reporter: danpalmer | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Adam (Chainz) Johnson):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:3>

Django

unread,
Aug 18, 2018, 9:34:03 AM8/18/18
to django-...@googlegroups.com
#29687: Support serialising JSON arrays in Django Test Client
-------------------------------------+-------------------------------------
Reporter: danpalmer | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by danpalmer):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:4>

Django

unread,
Aug 18, 2018, 9:35:26 AM8/18/18
to django-...@googlegroups.com
#29687: Support serialising JSON types other than objects in Django Test Client
-------------------------------------+-------------------------------------
Reporter: danpalmer | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by danpalmer:

Old description:

> This is a follow up to #29082, which added support for serialising JSON
> objects in the Django Test Client.
>
> Currently JSON arrays 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.

New description:

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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:5>

Django

unread,
Aug 18, 2018, 10:02:55 AM8/18/18
to django-...@googlegroups.com
#29687: Make the test client serialize JSON types other than dict
--------------------------------------+------------------------------------
Reporter: danpalmer | Owner: nobody
Type: Cleanup/optimization | Status: new

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:6>

Django

unread,
Aug 18, 2018, 10:14:28 AM8/18/18
to django-...@googlegroups.com
#29687: Make the test client serialize JSON types other than dict
--------------------------------------+------------------------------------
Reporter: danpalmer | Owner: nobody
Type: Cleanup/optimization | Status: new

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by danpalmer):

So, while implementing I've found a minor edge case.

From the Python docs
[https://docs.python.org/3/library/json.html#json.JSONEncoder 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 [https://github.com/python/cpython/blob/3.5/Lib/json/encoder.py
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:7>

Django

unread,
Aug 18, 2018, 10:15:39 AM8/18/18
to django-...@googlegroups.com
#29687: Make the test client serialize JSON types other than dict
--------------------------------------+------------------------------------
Reporter: danpalmer | Owner: nobody
Type: Cleanup/optimization | Status: new

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by danpalmer):

[https://github.com/django/django/pull/10313 PR]

Updated with full support, modulo the issue mentioned above.

--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:8>

Django

unread,
Aug 25, 2018, 10:56:43 AM8/25/18
to django-...@googlegroups.com
#29687: Make the test client serialize list/tuple for JSON mime type
-------------------------------------+-------------------------------------
Reporter: Dan Palmer | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:9>

Django

unread,
Aug 25, 2018, 11:20:55 AM8/25/18
to django-...@googlegroups.com
#29687: Make the test client serialize list/tuple for JSON mime type
-------------------------------------+-------------------------------------
Reporter: Dan Palmer | Owner: nobody
Type: | Status: closed
Cleanup/optimization |

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"e1816669732c54c40122c8a22dcba42f6ee3c326" e1816669]:
{{{
#!CommitTicketReference repository=""
revision="e1816669732c54c40122c8a22dcba42f6ee3c326"
Fixed #29687 -- Allowed the test client to serialize list/tuple as JSON.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:10>

Reply all
Reply to author
Forward
0 new messages