Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Fix schema with relationship to dump None #302

Merged

Conversation

mahenzon
Copy link
Contributor

@mahenzon mahenzon commented Apr 21, 2020

Caused by #278

When None is passed to a schema, which contains relationships, it fails. Though on the previous versions it successfully returned data. That's because of direct use of self._serialize. The check for missing fields just doesn't happen.

If you try to run the new test, it will fail with this text:

=================================== FAILURES ===================================
_____ TestResponseFormatting.test_schema_with_relationship_processes_none ______

self = <tests.test_schema.TestResponseFormatting object at 0x7f5ede746278>

    def test_schema_with_relationship_processes_none(self):
>       data = unpack(CommentSchema().dump(None))

test_schema.py:103: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../venv/lib/python3.6/site-packages/marshmallow/schema.py:553: in dump
    result = self._serialize(processed_obj, many=many)
../venv/lib/python3.6/site-packages/marshmallow/schema.py:517: in _serialize
    value = field_obj.serialize(attr_name, obj, accessor=self.get_attribute)
../marshmallow_jsonapi/fields.py:249: in serialize
    return self._serialize(None, attr, obj)
../marshmallow_jsonapi/fields.py:256: in _serialize
    related_url = self.get_related_url(obj)
../marshmallow_jsonapi/fields.py:155: in get_related_url
    params = resolve_params(obj, self.related_url_kwargs, default=self.default)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

obj = None, params = {'id': '<id>'}, default = <marshmallow.missing>

    def resolve_params(obj, params, default=missing):
        """Given a dictionary of keyword arguments, return the same dictionary except with
        values enclosed in `< >` resolved to attributes on `obj`.
        """
        param_values = {}
        for name, attr_tpl in params.items():
            attr_name = tpl(str(attr_tpl))
            if attr_name:
                attribute_value = get_value(obj, attr_name, default=default)
                if attribute_value is not missing:
                    param_values[name] = attribute_value
                else:
                    raise AttributeError(
                        "{attr_name!r} is not a valid "
>                       "attribute of {obj!r}".format(attr_name=attr_name, obj=obj)
                    )
E                   AttributeError: 'id' is not a valid attribute of None

../marshmallow_jsonapi/utils.py:55: AttributeError
============================== 1 failed in 0.25s ===============================

.gitignore Outdated Show resolved Hide resolved
@mahenzon
Copy link
Contributor Author

ouch, tests failed. do you really still support marshmallow 2? ok then, I'll make the test compatible..

@mahenzon mahenzon force-pushed the fix-relationship-field-dump-none branch from cda8f49 to 1db3eea Compare April 21, 2020 17:16
@mahenzon mahenzon changed the title Fix schema with relationship dump None Fix schema with relationship to dump None Apr 21, 2020
@iamareebjamal
Copy link
Contributor

Ping @sloria

Copy link
Member

@sloria sloria left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay on this. Looks good. I'll go ahead and update this with the latest dev.

.gitignore Outdated Show resolved Hide resolved
@sloria sloria merged commit ee7a50f into marshmallow-code:dev Dec 27, 2020
woodb pushed a commit to DataDog/marshmallow-jsonapi that referenced this pull request May 13, 2022
* Fix schema with relationship to dump None

* Revert gitignore change (.idea belongs in global gitignore)

* Fix test

* Update changelog

Co-authored-by: Steven Loria <sloria1@gmail.com>
woodb pushed a commit to DataDog/marshmallow-jsonapi that referenced this pull request May 16, 2022
* Fix schema with relationship to dump None

* Revert gitignore change (.idea belongs in global gitignore)

* Fix test

* Update changelog

Co-authored-by: Steven Loria <sloria1@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants