Skip to content

Commit 081d3a2

Browse files
committed
Handling RelationshipView
1 parent dc64c20 commit 081d3a2

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

rest_framework_json_api/parsers.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Parsers
33
"""
44
from rest_framework import parsers
5+
from rest_framework.exceptions import ParseError
56

67
from . import utils, renderers, exceptions
78

@@ -31,12 +32,20 @@ def parse(self, stream, media_type=None, parser_context=None):
3132
Parses the incoming bytestream as JSON and returns the resulting data
3233
"""
3334
result = super(JSONParser, self).parse(stream, media_type=media_type, parser_context=parser_context)
34-
data = result.get('data', {})
35+
data = result.get('data')
3536

3637
if data:
3738
from rest_framework_json_api.views import RelationshipView
3839
if isinstance(parser_context['view'], RelationshipView):
39-
return data # temporary workaround
40+
# We skip parsing the object as JSONAPI Resource Identifier Object and not a regular Resource Object
41+
if isinstance(data, list):
42+
for resource_identifier_object in data:
43+
if not (resource_identifier_object.get('id') and resource_identifier_object.get('type')):
44+
raise ParseError('Received data contains a malformed JSONAPI Resource Identifier Object')
45+
elif not (data.get('id') and data.get('type')):
46+
raise ParseError('Received data is not a valid JSONAPI Resource Identifier Object')
47+
48+
return data
4049
# Check for inconsistencies
4150
resource_name = utils.get_resource_name(parser_context)
4251
if data.get('type') != resource_name:
@@ -70,3 +79,6 @@ def parse(self, stream, media_type=None, parser_context=None):
7079
parsed_data.update(attributes)
7180
parsed_data.update(parsed_relationships)
7281
return parsed_data
82+
83+
else:
84+
raise ParseError('Received document does not contain primary data')

rest_framework_json_api/renderers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
5555

5656
json_api_included = list()
5757

58-
# If detail view then json api spec expects dict, otherwise a list
59-
# - http://jsonapi.org/format/#document-top-level
60-
# The `results` key may be missing if unpaginated or an OPTIONS request
61-
if view and hasattr(view, 'action') and view.action == 'list' and \
58+
from rest_framework_json_api.views import RelationshipView
59+
if isinstance(view, RelationshipView):
60+
# Special case for RelationshipView
61+
json_api_data = data
62+
elif view and hasattr(view, 'action') and view.action == 'list' and \
6263
isinstance(data, dict) and 'results' in data:
64+
# If detail view then json api spec expects dict, otherwise a list
65+
# - http://jsonapi.org/format/#document-top-level
66+
# The `results` key may be missing if unpaginated or an OPTIONS request
6367

6468
results = data["results"]
6569

0 commit comments

Comments
 (0)