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

If a relation is null return a 404 instead of throwing exception #254

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions flask_restless/views.py
Expand Up @@ -1006,6 +1006,8 @@ def get(self, instid, relationname, relationinstid):
result = self._paginated(list(related_value), deep)
else:
result = to_dict(related_value, deep)
if result is None:
abort(404)
for postprocessor in self.postprocessors['GET_SINGLE']:
postprocessor(result=result)
return jsonpify(result)
Expand Down
12 changes: 11 additions & 1 deletion tests/test_views.py
Expand Up @@ -107,9 +107,10 @@ def test_get(self):
owner = self.User()
pet1 = self.Pet()
pet2 = self.Pet()
pet3 = self.Pet()
pet1.owner = owner
pet2.owner = owner
self.db.session.add_all([owner, pet1, pet2])
self.db.session.add_all([owner, pet1, pet2, pet3])
self.db.session.commit()

response = self.app.get('/api/user/%d' % owner.id)
Expand Down Expand Up @@ -147,6 +148,15 @@ def test_get(self):
assert not isinstance(data['owner'], list)
assert owner.id == data['ownerid']

# Check that it's possible to get owner if not null
response = self.app.get('/api/pet/1/owner')
assert 200 == response.status_code
data = loads(response.data)
assert 2 == len(data['pets'])
# And that we get a 404 if owner is null
response = self.app.get('/api/pet/3/owner')
assert 404 == response.status_code


# skip_unless should be used as a decorator, but Python 2.5 doesn't have
# decorators.
Expand Down