biosustain / potion

Flask-Potion is a RESTful API framework for Flask and SQLAlchemy, Peewee or MongoEngine
http://potion.readthedocs.io
Other
488 stars 51 forks source link

Nullable is not required #164

Closed ticosax closed 5 years ago

ticosax commented 5 years ago

Avoid transmitting None values when not necessary

Consider a nullable field, not required without default, it shouldn't be given as None if absent. This is per definition an optional field, and should stay out of list of given input values if user did so.

This is useful to distinguish omitted fields from the ones given by users.

coveralls commented 5 years ago

Coverage Status

Coverage increased (+0.5%) to 89.148% when pulling 3eb8a170d93696b713d39ff6108c3a6cefb077f5 on ticosax:nullable-is-not-required into 296d244a18d0a91f46cd8152251ebed67831a7f9 on biosustain:master.

lyschoening commented 5 years ago

There are many situations where there is a difference between None and a missing field. It's an important and useful distinction. While a philosophical argument could be made that they should be treated equally by an API, that is a design decision that cannot easily be reverted. I don't see a good reason right now to break compatibility.

ticosax commented 5 years ago

Let's say you define some defaults on the signature of your constructor

class Model:
    def __init__(self, foo='a',**kwargs):
        ...

With the current implementation, if parameter foo is omitted by the user the model will always see None instead of a. This is contradict the expectations one would make. As a workaround we need to define the same default value also on the Resource. Which might become out of sync if we forgot. Not nice and error prone.