[Django] #33219: django.test.client missconversion of json with internal dicts

14 views
Skip to first unread message

Django

unread,
Oct 22, 2021, 6:57:54 AM10/22/21
to django-...@googlegroups.com
#33219: django.test.client missconversion of json with internal dicts
-------------------------------------+-------------------------------------
Reporter: caperutxa | Owner: nobody
Type: Bug | Status: new
Component: Testing | Version: 3.2
framework | Keywords: test.client,
Severity: Normal | complex json
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
django.test.client is used to tests post resquest in a django
restframework application with json as file content.

For basic json works : {'name': 'Mateu', 'number': 25}

But if the json has internal dictionaries it is transformed to an array:
From {'name': 'Mateu', 'number': 25, sales: {'Hubble':25, 'Web':4,
'telescope': 0}}
To <QueryDict: {'name': 'Mateu', 'number': 25, sales: ['Hubble', 'Web',
'telescope']}
Then it is not possible to iterate over all sales key-value pairs

Also, if in the code a sales list is requested, it give only the last
value (in that case telescope)

sales = request.data['sales'] --> sales will be 'telescope' instead of an
array at least

NOTE, that starting the real server and run a request via curl or via web
browser, it works as expected (a json with internal dictionaries or
arrays)

----

django-cors-headers 3.10.0
django-extensions 3.1.3
django-storages 1.11.1
djangorestframework 3.12.4
pytest-django 4.4.0

linux system (ubuntu)

----

Sample code (set the urls yourself)

Inside views

{{{
@api_view(['POST'])
def store_iteration(request):
print(request.data)
print(request.data['sales'])
}}}

Inside tests

{{{
class IterationsTestCase(TestCase):
def setUp(self):
self.client = Client()

def test_fake_up(self):
response = self.client.post('/api/test_it',
{'name': 'Mateu', 'number': 25, sales:
{'Hubble':25, 'Web':4, 'telescope': 0}},
format='json')
}}}

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

Django

unread,
Oct 22, 2021, 7:20:40 AM10/22/21
to django-...@googlegroups.com
#33219: django.test.client missconversion of json with internal dicts
-------------------------------------+-------------------------------------
Reporter: caperutxa | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: test.client, | Triage Stage:
complex json | Unreviewed
Has patch: 0 | Needs documentation: 0

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

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


Comment:

Thanks for the report, however I don't see any issue in Django here.
`format` is not an argument of
[https://docs.djangoproject.com/en/3.2/topics/testing/tools/#django.test.Client.post
Client.post()], you should use `content_type='application/json'` instead.

If you're having trouble understanding how Django works, see
TicketClosingReasons/UseSupportChannels for ways to get help.

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

Django

unread,
Oct 22, 2021, 7:30:36 AM10/22/21
to django-...@googlegroups.com
#33219: django.test.client missconversion of json with internal dicts
-------------------------------------+-------------------------------------
Reporter: caperutxa | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: test.client, | Triage Stage:
complex json | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by caperutxa):

It is solved using content_type='application/json'

https://stackoverflow.com/a/14669551/9794948

Please, could you update it in your documentation.

Regards

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

Django

unread,
Oct 22, 2021, 7:37:37 AM10/22/21
to django-...@googlegroups.com
#33219: django.test.client missconversion of json with internal dicts
-------------------------------------+-------------------------------------
Reporter: caperutxa | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: test.client, | Triage Stage:
complex json | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

> Please, could you update it in your documentation.

What do you mean? `format` is not mentioned in Django docs.

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

Django

unread,
Oct 22, 2021, 9:34:55 AM10/22/21
to django-...@googlegroups.com
#33219: django.test.client missconversion of json with internal dicts
-------------------------------------+-------------------------------------
Reporter: caperutxa | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: test.client, | Triage Stage:
complex json | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson):

This looks like a confusion with Django REST Framework's `APIClient`,
which does accept the `format` parameter.
The [https://www.django-rest-framework.org/api-guide/testing/#apiclient
docs for that a here].

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

Django

unread,
Oct 22, 2021, 5:54:06 PM10/22/21
to django-...@googlegroups.com
#33219: django.test.client missconversion of json with internal dicts
-------------------------------------+-------------------------------------
Reporter: Caperutxa | Owner: nobody

Type: Bug | Status: closed
Component: Testing framework | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: test.client, | Triage Stage:
complex json | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Caperutxa):

I mean the parameter `content_type='application/json'` is not mentioned in
the documentation like that

https://docs.djangoproject.com/en/3.2/topics/testing/tools/

There are a lot of uses of the client without any content_type parameter
such as

{{{
>>> c.post('/login/?visitor=true', {'name': 'fred', 'passwd': 'secret'})
}}}

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

Reply all
Reply to author
Forward
0 new messages