Skip to content

Add option to specify default included resources #250

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

knaperek
Copy link
Contributor

@knaperek knaperek commented Jun 3, 2016

Let's provide an extra JSONAPIMeta configuration option that specifies
resources to be listed in the 'included' section of the response.

These resources shall be included even if they're not explicitly mentioned
in the 'include' request parameter.

Let's provide an extra JSONAPIMeta configuration option that specifies
resources to be listed in the 'included' section of the response.
These resources shall be included even if they're not explicitly mentioned
in the 'include' request parameter.
@knaperek knaperek force-pushed the add-option-default-included-resources branch from 3e996be to 1f924ac Compare June 3, 2016 12:21
The previous build failure was caused by some
random environment issue.
included_resources = utils.get_default_included_resources_from_serializer(serializer)
include_resources_param = request.query_params.get('include') if request else None
if include_resources_param:
extra = filter(lambda r: r not in included_resources, include_resources_param.split(','))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this will result in behavior compliant with the spec. The spec states that "If an endpoint supports the include parameter and a client supplies it, the server MUST NOT include unrequested resource objects in the included section". So we should build included_resources from JSONAPIMeta.included_resources for a default but if include exists in the url then its value should be used instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. fixed.

Conform to the specs and ensure that no other resources are included
in the response other than those explicitely requested inside the
'include' parameter in request.
@scottfisk
Copy link
Contributor

So you are implementing this MAY functionality from JSON api spec? That sounds good to me. 👍

Seems like we have a couple options for configuring it:

  1. Using the JSONAPIMeta on serializer (like you have done here)
  2. Define something in the included dictionary for that field like include_by_default=True
  3. More implicitly include it like the default drf style where full objects are included if you define the field as a nested serializer.

@jerel Might have opinions on this, I am leaning towards 1 or 2. (2 might fit the existing implementation of includes better, idk)

@jerel
Copy link
Member

jerel commented Jun 3, 2016

Now that you mention option 2 that sounds slightly superior to adding included_resources to JSONAPIMeta because it's right at the point where the relationship is defined. Off the top of my head I'm not sure how painful/easy it would be to implement. Are there any downsides to include_by_default=True?

@knaperek
Copy link
Contributor Author

Maybe one downside: you'd have to declare the field manually, unlike in the case of the solution I initially propose, which is coherent with many Django settings in Admin, for example, when you list fields that are to be included on various places (list_display, readonly_fields, list_editable, list_filter, search_fields, radio_fields, etc...).

@knaperek
Copy link
Contributor Author

Another argument supporting the solution number 1 is this:
Let's say we have 3 models with their relation like this:
Comment -> Post -> User

We have defined serializers for all of them and want the root serializer (the UserSerializer) to automatically include its related resources. If we only wanted to inlcude the posts, both number 1 and 2 solutions could be seen as fit.

What if we also wanted to include all the related comments though (second level depth)? In that case I think it's much nicer to use the number 1 approach and define the UserSerializer class like this:

class UserSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=200)
    posts = PostSerializer(many=True, read_only=True)

    class JSONAPIMeta:
        resource_name = 'user'
        included_resources = ['posts.comments']

@jerel
Copy link
Member

jerel commented Jun 16, 2016

I think that going with included_resources in JSONAPIMeta is probably the best route. @knaperek are you ready for this to be merged then? We should have some test coverage on this feature yet.

@knaperek
Copy link
Contributor Author

Some tests added, waiting for merge. Thanks.

if include_resources_param:
included_resources = include_resources_param.split(',')
else:
included_resources = utils.get_default_included_resources_from_serializer(serializer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to make sure the include param should override the defaults, which is how you implemented it here.

If an endpoint supports the include parameter and a client supplies it, the server MUST NOT include unrequested resource objects in the included section of the compound document.

👍 , looks like this is all good.

@scottfisk
Copy link
Contributor

lgtm 👍

@scottfisk scottfisk merged commit c033db9 into django-json-api:develop Jun 23, 2016
peterkajan pushed a commit to peterkajan/django-rest-framework-json-api that referenced this pull request Jul 1, 2016
* Add option to specify default included resources

Let's provide an extra JSONAPIMeta configuration option that specifies
resources to be listed in the 'included' section of the response.
These resources shall be included even if they're not explicitly mentioned
in the 'include' request parameter.

* Re-trigger Travis Build script

The previous build failure was caused by some
random environment issue.

* Ignore default 'included' resources if specified in request

Conform to the specs and ensure that no other resources are included
in the response other than those explicitely requested inside the
'include' parameter in request.

* Add tests for default included_resources feature
martinsmid pushed a commit to martinsmid/django-rest-framework-json-api that referenced this pull request Nov 5, 2018
* Add option to specify default included resources

Let's provide an extra JSONAPIMeta configuration option that specifies
resources to be listed in the 'included' section of the response.
These resources shall be included even if they're not explicitly mentioned
in the 'include' request parameter.

* Re-trigger Travis Build script

The previous build failure was caused by some
random environment issue.

* Ignore default 'included' resources if specified in request

Conform to the specs and ensure that no other resources are included
in the response other than those explicitely requested inside the
'include' parameter in request.

* Add tests for default included_resources feature
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.

3 participants