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

Unclear error message on post_collection with OneToMany relationship #42

Open
kaitj91 opened this issue Mar 17, 2017 · 0 comments
Open

Comments

@kaitj91
Copy link
Contributor

kaitj91 commented Mar 17, 2017

For background information let's look at this test:

    def test_add_resource_with_invalid_relationships(self):
        """Create resource with invalid relationship returns 400.

        A BadRequestError is raised.
        """
        payload = {
            'data': {
                'attributes': {
                    'first': 'Sally',
                    'last': 'Smith',
                    'username': 'SallySmith1',
                    'password': 'password',
                },
                'type': 'users',
                'relationships': {
                    'posts': {
                        'data': {
                            'type': 'posts',
                            'id': 1
                        }
                    }
                }
            }
        }

        with self.assertRaises(errors.BadRequestError) as error:
            models.serializer.post_collection(
                self.session, payload, 'users')

        self.assertEqual(error.exception.detail, 'posts must be an array')
        self.assertEqual(error.exception.status_code, 400)

As we can see the error message produced by the library is somewhat confusing.
If we actually look at the check that is occuring it is:

                    if not isinstance(data_rel, list):
                        raise BadRequestError(
                            '{} must be an array'.format(key)

This is interesting because if we actually look at what data_rel is, it is defined as we see by this chunk of code:

                data_rel = data['data']['relationships'][api_key]
                if 'data' not in data_rel.keys():
                    raise BadRequestError(
                        'Missing data key in relationship {}'.format(key))
                data_rel = data_rel['data']

so data_rel is actually {'type': 'posts', 'id': 1}. Thus when the check to see if data_rel is a list, it is not but the detail of the BadRequestError is saying that the 'posts must be an array', since 'posts' is the key.

I believe that this detail message should be changed to say 'posts data must be an array'.
This might be trivial for someone that knows jsonapi well. However, if someone were trying to simply figure out how to format their response correctly after receiving this error message it would be confusing.

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

1 participant