Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

amw
Copy link
Contributor

@amw amw commented Dec 13, 2017

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 use data like a dictionary even when it wasn't one.

Related issue by another user: #367

  • Less exceptions caused by data type assumptions
  • Better handling of list data
  • Does not special-case RelationshipView
  • Support meta in Resource Identifier Objects (per spec)

@codecov-io
Copy link

codecov-io commented Dec 13, 2017

Codecov Report

Merging #393 into master will decrease coverage by 0.06%.
The diff coverage is 90%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
rest_framework_json_api/parsers.py 93.97% <90%> (-3.17%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e501b04...277990f. Read the comment docs.

@mblayman
Copy link
Collaborator

@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.

@amw
Copy link
Contributor Author

amw commented Dec 18, 2017

Sure, I'll add some tests. Just wanted to wait for initial feedback. I might have a bit of time during the holiday break.

@sliverc
Copy link
Member

sliverc commented Dec 22, 2017

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 DEFAULT_PARSER_CLASSES and extend views with mixin which actually need bulk support. In the respective class it could be clearly documented that this is an experimental feature.

What do you think?

- Less exceptions caused by data type assumptions
- Better handling of list data
- Does not special-case RelationshipView
@sliverc sliverc self-requested a review May 1, 2018 11:33
Copy link
Member

@sliverc sliverc left a 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?

@sliverc
Copy link
Member

sliverc commented Oct 25, 2018

@amw
There hasn't been any progress on this since quite a while so I am closing this PR. If you still want to work on this please comment and I can reopen. Or if someone else wants to work on this change feel free to open a new PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants