Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d5d7215

Browse files
bpleshakovsliverc
authored andcommitted
Properly return parser error when primary data is of invalid type (#762)
If you submit a list with one object, for example, DJA will respond you with unobvious errors.
1 parent 96aaa13 commit d5d7215

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ any parts of the framework not mentioned in the documentation should generally b
1919
### Fixed
2020

2121
* 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.
22+
* Properly return parser error when primary data is of invalid type
2223

2324
## [3.0.0] - 2019年10月14日
2425

‎example/tests/test_parsers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,20 @@ def test_parse_invalid_data(self):
5252

5353
with self.assertRaises(ParseError):
5454
parser.parse(stream, None, self.parser_context)
55+
56+
def test_parse_invalid_data_key(self):
57+
parser = JSONParser()
58+
59+
string = json.dumps({
60+
'data': [{
61+
'id': 123,
62+
'type': 'Blog',
63+
'attributes': {
64+
'json-value': {'JsonKey': 'JsonValue'}
65+
},
66+
}]
67+
})
68+
stream = BytesIO(string.encode('utf-8'))
69+
70+
with self.assertRaises(ParseError):
71+
parser.parse(stream, None, self.parser_context)

‎rest_framework_json_api/parsers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ def parse(self, stream, media_type=None, parser_context=None):
116116

117117
request = parser_context.get('request')
118118

119+
# Sanity check
120+
if not isinstance(data, dict):
121+
raise ParseError('Received data is not a valid JSONAPI Resource Identifier Object')
122+
119123
# Check for inconsistencies
120124
if request.method in ('PUT', 'POST', 'PATCH'):
121125
resource_name = utils.get_resource_name(

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /