flask-restx
flask-restx copied to clipboard
Wildcard dictionary omits keys with null values
The marshalled output of a model with wildcard field omits any member of the input dictionary whose value is None or whose key "comes before" a key whose value is None.
Repro Steps
- Run this:
from flask_restx import marshal
from flask_restx.fields import Wildcard, String
wild = Wildcard(String)
model = {'*': wild}
data = {'a': 1, 'b': '2', 'c': None, 'd': 20, 'e': 'e'}
print('data:', data)
marshalled = marshal(data, model)
print('marshalled:', marshalled)
assert len(data) == len(marshalled)
Expected Behavior
Marshalled response should include all fields from data.
./test.py
data: {'a': 1, 'b': '2', 'c': None, 'd': 20, 'e': 'e'}
marshalled: {'e': 'e', 'd': '20', 'c': None, 'b': '2', 'a': '1'}
Actual Behavior
The field with null value and keys that come before it are missing.
./test.py
data: {'a': 1, 'b': '2', 'c': None, 'd': 20, 'e': 'e'}
marshalled: {'e': 'e', 'd': '20'}
Traceback (most recent call last):
File "./test.py", line 10, in <module>
assert len(data) == len(marshalled)
AssertionError
Environment
Python 3.8.5 Flask 0.12.5 flask-restx 0.5.1 Flask-SQLAlchemy 2.5.1
Additional Context
I have a "fix" for this, might be incorrect, will submit PR tomorrow. Similar to https://github.com/python-restx/flask-restx/issues/283, but not sure fix will be the same.