From c8511a07b2cd456cd5701f3d49391d9d0183c36d Mon Sep 17 00:00:00 2001 From: Alexander Seidmann <40206670+aseidma@users.noreply.github.com> Date: Sat, 20 Aug 2022 15:09:56 +0200 Subject: [PATCH] Added a separate parse_data method to JSONParser (#1083) Co-authored-by: Alex Seidmann Co-authored-by: Oliver Sauder --- AUTHORS | 1 + CHANGELOG.md | 1 + rest_framework_json_api/parsers.py | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/AUTHORS b/AUTHORS index 419144b5..15a5393f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,6 +1,7 @@ Adam Wróbel Adam Ziolkowski Alan Crosswell +Alex Seidmann Anton Shutik Ashley Loewen Asif Saif Uddin diff --git a/CHANGELOG.md b/CHANGELOG.md index 91551dca..41990d56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ any parts of the framework not mentioned in the documentation should generally b ### Added * Added support for Django 4.1. +* Expanded JSONParser API with `parse_data` method ### Removed diff --git a/rest_framework_json_api/parsers.py b/rest_framework_json_api/parsers.py index f77a6501..2aef1aa1 100644 --- a/rest_framework_json_api/parsers.py +++ b/rest_framework_json_api/parsers.py @@ -70,14 +70,11 @@ def parse_metadata(result): else: return {} - def parse(self, stream, media_type=None, parser_context=None): + def parse_data(self, result, parser_context): """ - Parses the incoming bytestream as JSON and returns the resulting data + Formats the output of calling JSONParser to match the JSON:API specification + and returns the result. """ - result = super().parse( - stream, media_type=media_type, parser_context=parser_context - ) - if not isinstance(result, dict) or "data" not in result: raise ParseError("Received document does not contain primary data") @@ -166,3 +163,13 @@ def parse(self, stream, media_type=None, parser_context=None): parsed_data.update(self.parse_relationships(data)) parsed_data.update(self.parse_metadata(result)) return parsed_data + + def parse(self, stream, media_type=None, parser_context=None): + """ + Parses the incoming bytestream as JSON and returns the resulting data + """ + result = super().parse( + stream, media_type=media_type, parser_context=parser_context + ) + + return self.parse_data(result, parser_context)