Skip to content

JSON:API 1.1: return empty 'included' array when ?include= is present #1301

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ coverage.xml

# VirtualEnv
.venv/
venv/



# Developers
*.sw*
Expand All @@ -51,3 +54,7 @@ manage.py

# example database
drf_example

pytest.ini


25 changes: 24 additions & 1 deletion example/tests/integration/test_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def test_missing_field_not_included(author_bio_factory, author_factory, client):
# First author does not have a bio
author = author_factory(bio=None)
response = client.get(reverse("author-detail", args=[author.pk]) + "?include=bio")
assert "included" not in response.json()
content = response.json()
assert "included" in content
assert content["included"] == []
# Second author does
author = author_factory()
response = client.get(reverse("author-detail", args=[author.pk]) + "?include=bio")
Expand Down Expand Up @@ -204,3 +206,24 @@ def test_meta_object_added_to_included_resources(single_entry, client):
)
assert response.json()["included"][0].get("meta")
assert response.json()["included"][1].get("meta")


def test_included_empty_array_when_requested(client, author_factory):
author = author_factory(bio=None) # explicitly ensure no related bio
url = reverse("author-detail", args=[author.pk]) + "?include=bio"
response = client.get(url)
assert response.status_code == 200

content = response.json()
assert "included" in content
assert content["included"] == []


def test_included_absent_when_not_requested(client, author_factory):
# Create an author (bio can be None or default, doesn't matter here)
author = author_factory(bio=None)
url = reverse("author-detail", args=[author.pk])
response = client.get(url)
assert response.status_code == 200
content = response.json()
assert "included" not in content
4 changes: 4 additions & 0 deletions rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,10 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
render_data["included"].append(
included_cache[included_type][included_id]
)
else:
request = renderer_context.get("request")
if request and "include" in request.query_params:
render_data["included"] = []

if json_api_meta:
render_data["meta"] = format_field_names(json_api_meta)
Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ exclude =
build/lib,
.eggs
.tox,
env
.venv
env,
.venv,
venv

[isort]
multi_line_output = 3
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ deps =
-rrequirements/requirements-documentation.txt
commands =
sphinx-build -W -b html -d docs/_build/doctrees docs docs/_build/html