Skip to content

Commit

Permalink
Resolve issue #304; retain previous behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdscharf committed May 26, 2021
1 parent a39e06f commit 0a9ec62
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
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

0 comments on commit 0a9ec62

Please sign in to comment.