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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,6 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
if resource_name == 'errors':
return self.render_errors(data, accepted_media_type, renderer_context)

include_resources_param = request.query_params.get('include') if request else None
if include_resources_param:
included_resources = include_resources_param.split(',')
else:
included_resources = list()

json_api_data = data
json_api_included = list()
# initialize json_api_meta with pagination meta or an empty dict
Expand All @@ -433,6 +427,13 @@ def render(self, data, accepted_media_type=None, renderer_context=None):

serializer = getattr(serializer_data, 'serializer', None)

# Build a list of included resources
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.

included_resources.extend(extra)

if serializer is not None:

# Get the serializer fields
Expand Down
7 changes: 7 additions & 0 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ def get_resource_type_from_serializer(serializer):
return get_resource_type_from_model(serializer.Meta.model)


def get_default_included_resources_from_serializer(serializer):
try:
return list(serializer.JSONAPIMeta.included_resources)
except AttributeError:
return []


def get_included_serializers(serializer):
included_serializers = copy.copy(getattr(serializer, 'included_serializers', dict()))

Expand Down