cloudendpoints / endpoints-python

A Python framework for building RESTful APIs on Google App Engine
Apache License 2.0
51 stars 17 forks source link

Empty values are not removed by endpoints #39

Open emelois opened 7 years ago

emelois commented 7 years ago

I think we've noticed a behavior which is not backward compatible with endpoints v1.

In v1, when the app is deployed (does not happen on the dev server), all empty values (None, (), {}, []) are removed from messages by the endpoints lib.

For instance :

d = {} d['first'] = 'one' d['two'] = {}

is transformed in {"first": "one"} in v1 and in {"first": "one", "two": {}} in v2.

Is this by design or is it a bug that will be fixed?

Here is the monkey patch we use:

from protorpc.protojson import MessageJSONEncoder

endpoints_original_method = MessageJSONEncoder.default

def del_none(d):
    """
    Delete keys with the value ``None`` in a dictionary, recursively.

    http://stackoverflow.com/questions/4255400/exclude-empty-null-values-from-json-serialization

    This alters the input so you may wish to ``copy`` the dict first.

    Args:
        d: the value to remove empty values from.
    """
    if isinstance(d, dict):
        for key, value in d.items():
            if value in (None, [], (), {}, ):
                del d[key]
            elif isinstance(value, dict):
                del_none(value)
    return d

def default(self, value):
    """
    Monkey patch the SDK method to remove empty values.
    Replicate the behavior of the prod endpoints.

    Args:
        value: Value to get dictionary for.
    """
    return del_none(endpoints_original_method(self, value))

MessageJSONEncoder.default = default
bradfriedman commented 7 years ago

Does this behavior persist if you assign an empty list or tuple? Looking through the code, it appears that the intent is to remove NoneType values and empty lists/tuples, but not empty dicts. However, if the v1 behavior was to remove empty dicts as well, I can continue to look into this. Thanks!

emelois commented 7 years ago

You're right, it seems to be the case only for empty dicts. I confirm that they were removed in v1.

nilleb commented 6 years ago

@bradfriedman anything new on this?

inklesspen commented 6 years ago

Brad is no longer on the team.

nilleb commented 6 years ago

Anyone any news about this?

Le mer. 8 nov. 2017 22:33, Rose Davidson notifications@github.com a écrit :

Brad is no longer on the team.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/cloudendpoints/endpoints-python/issues/39#issuecomment-342967155, or mute the thread https://github.com/notifications/unsubscribe-auth/ADVmx0Ll_YIlqyeoIFbGdw3DAtedWP2sks5s0h4dgaJpZM4Lspe8 .

inklesspen commented 6 years ago

I have only just seen this issue. I will look into it.

lcasartelli commented 6 years ago

+1 any news on this? I am experiencing the same behavior.