-
Notifications
You must be signed in to change notification settings - Fork 301
Overall improvements to parser. #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov Report
@@ Coverage Diff @@
## master #393 +/- ##
==========================================
- Coverage 91.75% 91.68% -0.07%
==========================================
Files 55 55
Lines 2923 2936 +13
==========================================
+ Hits 2682 2692 +10
- Misses 241 244 +3
Continue to review full report at Codecov.
|
@amw, thanks, this looks pretty cool. If I'm reading your description correctly, this is adding new behavior to the JSONParser. Do you think some new tests could be included to hit some of the extra lines in the parser? Looking at the codecov report, lines 127 and 139 do not appear to be covered. |
Sure, I'll add some tests. Just wanted to wait for initial feedback. I might have a bit of time during the holiday break. |
Thanks @amw for bringing this PR forward. I have also struggled with bulk operation support with json:api for a while and have been following the different discussions. What you are suggesting is to implement the experimental bulk extension as described here This seems to have been deprecated and json:api is properly moving to a completely different approach in version 1.1 as discussed in json-api/json-api#1197 I think though it is still legit to implement bulk extension in DJA as DJA currently targets json:api version "1.0". I think though as it is an experimental feature and media type is different ('application/vnd.api+json; ext=bulk') bulk support logic should be separated into its own parser. Reason being as the standard doesn't allow list primary data when creating or updating resources so the jsonapi parser shouldn't allow it either. Instead a bulk parser could be designed like this: import io
import json
from django.conf import settings
from rest_framework_json_api import parsers
class JSONAPIBulkParser(parsers.JSONParser):
media_type = 'application/vnd.api+json; ext=bulk'
def parse(self, stream, media_type=None, parser_context=None):
data = []
encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
data_collection = json.loads(stream.read().decode(encoding))
for single_data in data_collection['data']:
single_data = {'data': single_data}
sub_data = super(JSONAPIBulkParser, self).parse(
io.BytesIO(bytes(json.dumps(single_data), encoding)),
media_type=media_type,
parser_context=parser_context
)
data.append(sub_data)
return data For this parser to work a view mixin would be needed which could look something like this: class BulkExtensionMixin(object):
def get_serializer(self, *args, **kwargs):
is_bulk = isinstance(self.request.data, list)
kwargs['many'] = kwargs.pop('many', is_bulk)
return super().get_serializer(*args, **kwargs) This way if a user of DJA wants to use the experimental bulk feature it would need to add new parser to What do you think? |
- Less exceptions caused by data type assumptions - Better handling of list data - Does not special-case RelationshipView
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this.
There are quite a few things happening in this PR and I think do get a better picture it would be better to split it up.
- One PR for better exception handling in parser
- and another PR for a special bulk parser as outlined in above comment
What do you think? Would you have time to work on this?
@amw |
This PR is something that I've mentioned long time ago in #290. It adds support for sending list data in views other than
RelationshipView
allowing users to create bulk operation views (Bulk operations are one of JSON API extensions). But it also fixes a few "500 Server Error" instances where parser tried to usedata
like a dictionary even when it wasn't one.Related issue by another user: #367
meta
in Resource Identifier Objects (per spec)