-
Notifications
You must be signed in to change notification settings - Fork 301
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
Add option to specify default included resources #250
Conversation
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.
3e996be
to
1f924ac
Compare
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(',')) |
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.
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.
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. 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.
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:
@jerel Might have opinions on this, I am leaning towards 1 or 2. (2 might fit the existing implementation of includes better, idk) |
Now that you mention option 2 that sounds slightly superior to adding |
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 ( |
Another argument supporting the solution number 1 is this: We have defined serializers for all of them and want the root serializer (the 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 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'] |
I think that going with |
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) |
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.
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.
lgtm 👍 |
* 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
* 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
Let's provide an extra
JSONAPIMeta
configuration option that specifiesresources 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.