python-restx / flask-restx

Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
https://flask-restx.readthedocs.io/en/latest/
Other
2.16k stars 335 forks source link

fields.Nested() does not respect 'skip_none=True' #478

Closed db0 closed 2 years ago

db0 commented 2 years ago

I have a model with fields.Nested() of another model. In the mashal_with() I have also set skip_none=True.

When that value is missing, it causes the marshaling to give me an dictionary where all the values are none, instead of omitting the dictionary completely or even just giving me an empty dictionary.

This is causing me issues because my return code differs depending on the instance, so sometimes my return will have some fields, and sometimes others. But marshalling forces all the fields to exist. And without marshal_with() I cannot get automatic documentation based on the models. I also send the dictionary values as **kwargs elsewhere, and them being filled with None (instead of not existing) is crashing the function.

If anyone has an idea for a workaround, please let me know

db0 commented 2 years ago

Welp, I just discovered that fields.Nested() has its OWN skip_none setting, which is set when the model is declared. Using THAT setting, allows me to finally achieve the result I want.