Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order is not preserved when OrderedDict is returned from view. #517

Open
mamalos opened this issue Sep 19, 2019 · 0 comments
Open

Order is not preserved when OrderedDict is returned from view. #517

mamalos opened this issue Sep 19, 2019 · 0 comments

Comments

@mamalos
Copy link
Contributor

mamalos commented Sep 19, 2019

With Cornice versions 3.6 and 3.7-dev, when returning an OrderedDict, the order of the parameters is not preserved in the returned request.

Ie. if we have the following code:

from cornice import Service
from collections import OrderedDict

test_ordered_service = Service(name='testorderedservice',
                    path='/testorderedservice',)

test_unordered_service = Service(name='testunorderedservice',
                    path='/testunorderedservice',)


@test_ordered_service.get()
def return_ordered_stuff(request):
    ret = OrderedDict([
        ('datefrom', '2019-07-17'),
        ('dateto', '2019-07-23'),
        ('origin', 'test'),
        ('overall_usage', []),
        ('pop_usage', {}),
        ('hit_usage', 5),
        ('bandwidth_usage', 3160)])
    return ret


@test_unordered_service.get()
def return_unordered_stuff(request):
    ret = {'datefrom': '2019-07-17',
           'dateto': '2019-07-23',
           'origin': 'test',
           'overall_usage': [],
           'pop_usage': {},
           'hit_usage': 5,
           'bandwidth_usage': 3160}
    return ret

Then we're getting the following results:

$ curl -X GET http://localhost:6543/testorderedservice | python -m json.tool 

{
    "bandwidth_usage": 3160,
    "datefrom": "2019-07-17",
    "dateto": "2019-07-23",
    "hit_usage": 5,
    "origin": "test",
    "overall_usage": [],
    "pop_usage": {}
}

$ curl -X GET http://localhost:6543/testunorderedservice | python -m json.tool

{
    "bandwidth_usage": 3160,
    "datefrom": "2019-07-17",
    "dateto": "2019-07-23",
    "hit_usage": 5,
    "origin": "test",
    "overall_usage": [],
    "pop_usage": {}
}

I realised this problem when I was running cornice with marshmallow validators, where my output was not in the expected order and I was suspecting it was a marshmallow problem, which eventually was not the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant