Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty values are not removed by endpoints #39

Open
emelois opened this issue Jan 24, 2017 · 7 comments
Open

Empty values are not removed by endpoints #39

emelois opened this issue Jan 24, 2017 · 7 comments
Assignees

Comments

@emelois
Copy link

emelois commented Jan 24, 2017

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
Copy link
Contributor

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
Copy link
Author

emelois commented Feb 2, 2017

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

@nilleb
Copy link

nilleb commented Nov 8, 2017

@bradfriedman anything new on this?

@inklesspen
Copy link
Contributor

Brad is no longer on the team.

@inklesspen inklesspen self-assigned this Nov 8, 2017
@nilleb
Copy link

nilleb commented Nov 8, 2017 via email

@inklesspen
Copy link
Contributor

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

@lcasartelli
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants