Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Fix errors in relationship views #288

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions example/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def test_patch_invalid_entry_relationship_blog_returns_400(self):
content_type='application/vnd.api+json')
assert response.status_code == 400

def test_relationship_view_errors_format(self):
url = '/entries/{}/relationships/blog'.format(self.first_entry.id)
response = self.client.patch(url,
data=json.dumps({'data': {'invalid': ''}}),
content_type='application/vnd.api+json')
assert response.status_code == 400

result = json.loads(response.content.decode('utf-8'))

assert 'data' not in result
assert 'errors' in result

def test_get_empty_to_one_relationship(self):
url = '/comments/{}/relationships/author'.format(self.first_entry.id)
response = self.client.get(url)
Expand Down
14 changes: 7 additions & 7 deletions rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,24 +419,24 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
view = renderer_context.get("view", None)
request = renderer_context.get("request", None)

# Get the resource name.
resource_name = utils.get_resource_name(renderer_context)

# If this is an error response, skip the rest.
if resource_name == 'errors':
return self.render_errors(data, accepted_media_type, renderer_context)

from rest_framework_json_api.views import RelationshipView
if isinstance(view, RelationshipView):
return self.render_relationship_view(data, accepted_media_type, renderer_context)

# Get the resource name.
resource_name = utils.get_resource_name(renderer_context)

# If `resource_name` is set to None then render default as the dev
# wants to build the output format manually.
if resource_name is None or resource_name is False:
return super(JSONRenderer, self).render(
data, accepted_media_type, renderer_context
)

# If this is an error response, skip the rest.
if resource_name == 'errors':
return self.render_errors(data, accepted_media_type, renderer_context)

json_api_data = data
json_api_included = list()
# initialize json_api_meta with pagination meta or an empty dict
Expand Down