|
2 | 2 | Parsers |
3 | 3 | """ |
4 | 4 | from rest_framework import parsers |
| 5 | +from rest_framework.exceptions import ParseError |
5 | 6 |
|
6 | 7 | from . import utils, renderers, exceptions |
7 | 8 |
|
@@ -31,12 +32,20 @@ def parse(self, stream, media_type=None, parser_context=None): |
31 | 32 | Parses the incoming bytestream as JSON and returns the resulting data |
32 | 33 | """ |
33 | 34 | result = super(JSONParser, self).parse(stream, media_type=media_type, parser_context=parser_context) |
34 | | - data = result.get('data', {}) |
| 35 | + data = result.get('data') |
35 | 36 |
|
36 | 37 | if data: |
37 | 38 | from rest_framework_json_api.views import RelationshipView |
38 | 39 | 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 |
40 | 49 | # Check for inconsistencies |
41 | 50 | resource_name = utils.get_resource_name(parser_context) |
42 | 51 | if data.get('type') != resource_name: |
@@ -70,3 +79,6 @@ def parse(self, stream, media_type=None, parser_context=None): |
70 | 79 | parsed_data.update(attributes) |
71 | 80 | parsed_data.update(parsed_relationships) |
72 | 81 | return parsed_data |
| 82 | + |
| 83 | + else: |
| 84 | + raise ParseError('Received document does not contain primary data') |
0 commit comments