It would be nice if there was a way to filter out any fields where the return value is None. For some responses, if a value is not present then it is better to not return the field all together rather than return a JSON null. Flask-RESTX provides this functionality in their marshal_with decorator, which allows a skip_none parameter to be passed in (docs). The proposed solution for this would be to allow the responds decorator to similarly take a skip_none bool parameter (defaulting to False), which would filter out any None value fields produced in the serialized object.
In this PR
Adds a skip_none boolean parameter to the responds decorator
If skip_none is set as True, then removes any fields that have a None value from the serialized object
None removal logic implemented recursively to handle for nested schemas
Resolves issue https://github.com/apryor6/flask_accepts/issues/99.
Why
It would be nice if there was a way to filter out any fields where the return value is
None
. For some responses, if a value is not present then it is better to not return the field all together rather than return a JSONnull
. Flask-RESTX provides this functionality in theirmarshal_with
decorator, which allows askip_none
parameter to be passed in (docs). The proposed solution for this would be to allow theresponds
decorator to similarly take askip_none
bool parameter (defaulting toFalse
), which would filter out anyNone
value fields produced in the serialized object.In this PR
skip_none
boolean parameter to theresponds
decoratorskip_none
is set asTrue
, then removes any fields that have aNone
value from the serialized objectNone
removal logic implemented recursively to handle for nested schemas