Skip to content

Issue #304: pass along ordered parameter #326

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

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion flask_restx/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,12 @@ def output(self, key, obj, ordered=False):
# we are using pop() so that we don't
# loop over the whole object every time dropping the
# complexity to O(n)
(objkey, val) = self._flat.pop()
if ordered:
# Get first element if respecting order
(objkey, val) = self._flat.pop(0)
else:
# Previous default retained
(objkey, val) = self._flat.pop()
if (
objkey not in self._cache
and objkey not in self.exclude
Expand Down
2 changes: 1 addition & 1 deletion flask_restx/marshalling.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def marshal(data, fields, envelope=None, skip_none=False, mask=None, ordered=Fal
if keys:
field.exclude |= set(keys)
keys = []
value = field.output(dkey, data)
value = field.output(dkey, data, ordered=ordered)
if is_wildcard:

def _append(k, v):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_marshalling.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@ def test_marshal_nested(self):

assert output == expected

def test_marshal_ordered(self):
model = OrderedDict(
[("foo", fields.Raw), ("baz", fields.Raw), ("bar", fields.Raw)]
)
marshal_fields = {
"foo": 1,
"baz": 2,
"bar": 3
}
expected_ordered = OrderedDict([("foo", 1), ("baz", 2), ("bar", 3)])
ordered_output = marshal(marshal_fields, model, ordered=True)
assert ordered_output == expected_ordered
unordered_output = marshal(marshal_fields, model)
assert not isinstance(unordered_output, OrderedDict)

def test_marshal_nested_ordered(self):
model = OrderedDict(
[("foo", fields.Raw), ("fee", fields.Nested({"fye": fields.String,}))]
Expand Down