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

nullable List field #107

Closed
shaicantor opened this issue May 29, 2016 · 2 comments

Comments

@shaicantor
Copy link

commented May 29, 2016

Hi,

When passing null in a List field, it is deserialized as [None]. (e.g. return [val])
Is there a way to for it to be deserialized as None without workarounds like pre_load or post_load decorators?

def get_value(data, name, field, allow_many_nested=False):
    """Get a value from a dictionary. Handles ``MultiDict`` types when
    ``multiple=True``. If the value is not found, return `missing`.

    :param object data: Mapping (e.g. :type:`dict`) or list-like instance to
        pull the value from.
    :param str name: Name of the key.
    :param bool multiple: Whether to handle multiple values.
    :param bool allow_many_nested: Whether to allow a list of nested objects
        (it is valid only for JSON format, so it is set to True in ``parse_json``
        methods).
    """
    missing_value = missing
    if allow_many_nested and isinstance(field, ma.fields.Nested) and field.many:
        if is_collection(data):
            return data
        missing_value = []

    if not hasattr(data, 'get'):
        return missing_value

    multiple = is_multiple(field)
    val = data.get(name, missing_value)
    if multiple and val is not missing:
        if hasattr(data, 'getlist'):
            return data.getlist(name)
        elif hasattr(data, 'getall'):
            return data.getall(name)
        elif isinstance(val, (list, tuple)):
            return val
        return [val]

    return val

@shaicantor shaicantor changed the title flaskparser nullable List field nullable List field May 29, 2016

@sloria

This comment has been minimized.

Copy link
Member

commented May 29, 2016

Thanks for reporting, @shaicantor . That certainly isn't the desired behavior; None should deserialize to None. The problem does appear to be in get_value, as you've suggested.

I would certainly review and merge a PR fixing this.

@sloria sloria closed this in 1a66d47 May 30, 2016

@sloria

This comment has been minimized.

Copy link
Member

commented May 30, 2016

This should now be fixed on dev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.