Marshaled nested wildcard field with ordered=True doesn't preserve dictionary key order.
Code
from flask_restx import fields, marshal
wild = fields.Wildcard(fields.String)
wildcard_fields = {'*': wild}
value = {'John': 12, 'bob': 42, 'Jane': '68'}
data = marshal(
value, wildcard_fields, ordered=True
)
print(data)
>> OrderedDict([('Jane', '68'), ('bob', '42'), ('John', '12')])
Repro Steps (if applicable)
- See above sample code
Expected Behavior
The marshaled dictionary's keys have the same order.
OrderedDict([('John', '12'), ('bob', '42'), ('Jane', '68')])
Actual Behavior
The marshaled dictionary's keys order is not preserved.
Environment
- Python 3.9
- Flask version 1.1.2
- Flask-RESTX version 0.3.0
- Other installed Flask extensions
Additional Context
Currently I'm sorting the dictionary keys with a custom field to solve my problem:
class CustomNested(fields.Nested):
def output(self, key, obj, ordered=True, **kwargs):
data = super().output(key, obj, ordered=ordered, **kwargs)
return OrderedDict(sorted(data.items()))
Marshaled nested wildcard field with
ordered=Truedoesn't preserve dictionary key order.Code
Repro Steps (if applicable)
Expected Behavior
The marshaled dictionary's keys have the same order.
Actual Behavior
The marshaled dictionary's keys order is not preserved.
Environment
Additional Context
Currently I'm sorting the dictionary keys with a custom field to solve my problem: