diff --git a/CHANGELOG.md b/CHANGELOG.md index 57cef3d5..e9b41f3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ any parts of the framework not mentioned in the documentation should generally b ### Fixed * Ensure that `409 Conflict` is returned when processing a `PATCH` request in which the resource object’s type and id do not match the server’s endpoint properly as outlined in [JSON:API](https://jsonapi.org/format/#crud-updating-responses-409) spec. +* Properly return parser error when primary data is of invalid type ## [3.0.0] - 2019-10-14 diff --git a/example/tests/test_parsers.py b/example/tests/test_parsers.py index 41ad05ac..6ff2cfa7 100644 --- a/example/tests/test_parsers.py +++ b/example/tests/test_parsers.py @@ -52,3 +52,20 @@ def test_parse_invalid_data(self): with self.assertRaises(ParseError): parser.parse(stream, None, self.parser_context) + + def test_parse_invalid_data_key(self): + parser = JSONParser() + + string = json.dumps({ + 'data': [{ + 'id': 123, + 'type': 'Blog', + 'attributes': { + 'json-value': {'JsonKey': 'JsonValue'} + }, + }] + }) + stream = BytesIO(string.encode('utf-8')) + + with self.assertRaises(ParseError): + parser.parse(stream, None, self.parser_context) diff --git a/rest_framework_json_api/parsers.py b/rest_framework_json_api/parsers.py index 5b024499..7a940b6c 100644 --- a/rest_framework_json_api/parsers.py +++ b/rest_framework_json_api/parsers.py @@ -116,6 +116,10 @@ def parse(self, stream, media_type=None, parser_context=None): request = parser_context.get('request') + # Sanity check + if not isinstance(data, dict): + raise ParseError('Received data is not a valid JSONAPI Resource Identifier Object') + # Check for inconsistencies if request.method in ('PUT', 'POST', 'PATCH'): resource_name = utils.get_resource_name(