Skip to content

Commit 0380b4a

Browse files
fix: ensuring empty array is added when requested and updated tests
1 parent 433794b commit 0380b4a

File tree

8 files changed

+50
-32
lines changed

8 files changed

+50
-32
lines changed

.gitignore

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ coverage.xml
4343

4444
# VirtualEnv
4545
.venv/
46-
venv/
47-
48-
4946

5047
# Developers
5148
*.sw*
@@ -55,6 +52,3 @@ manage.py
5552
# example database
5653
drf_example
5754

58-
pytest.ini
59-
60-

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ David Guillot, for Contexte <[email protected]>
1515
David Vogt <[email protected]>
1616
Felix Viernickel <[email protected]>
1717
Greg Aker <[email protected]>
18+
Harshal Kalewar <[email protected]>
1819
Humayun Ahmad <[email protected]>
1920
Jamie Bliss <[email protected]>
2021
Jason Housley <[email protected]>

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Changelog
1+
# _Changelog
22

33
All notable changes to this project will be documented in this file.
44

@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
Note that in line with [Django REST framework policy](https://www.django-rest-framework.org/topics/release-notes/),
99
any parts of the framework not mentioned in the documentation should generally be considered private API, and may be subject to change.
1010

11+
## [Unreleased]
12+
13+
### Fixed
14+
15+
* Ensured that an empty `included` array is returned in responses when the `include` query parameter is present but no related resources exist.
16+
1117
## [8.0.0] - 2025-07-24
1218

1319
### Added
@@ -31,7 +37,7 @@ any parts of the framework not mentioned in the documentation should generally b
3137
* Removed support for Django 5.0.
3238
* Removed built-in support for generating OpenAPI schema. Use [drf-spectacular-json-api](https://github.com/jokiefer/drf-spectacular-json-api/) instead.
3339

34-
## [7.1.0] - 2024-10-25
40+
## [7.1.0] - 2024-10-25_
3541

3642
This is the last release supporting Python 3.8, Django 5.0 and Django REST framework 3.14.
3743

example/tests/integration/test_includes.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,22 +208,45 @@ def test_meta_object_added_to_included_resources(single_entry, client):
208208
assert response.json()["included"][1].get("meta")
209209

210210

211-
def test_included_empty_array_when_requested(client, author_factory):
212-
author = author_factory(bio=None) # explicitly ensure no related bio
213-
url = reverse("author-detail", args=[author.pk]) + "?include=bio"
214-
response = client.get(url)
215-
assert response.status_code == 200
216-
211+
def test_included_array_empty_when_requested_but_no_data(blog_factory, client):
212+
blog = blog_factory()
213+
response = client.get(
214+
reverse("blog-detail", kwargs={"pk": blog.pk}) + "?include=tags"
215+
)
217216
content = response.json()
217+
218218
assert "included" in content
219219
assert content["included"] == []
220220

221221

222-
def test_included_absent_when_not_requested(client, author_factory):
223-
# Create an author (bio can be None or default, doesn't matter here)
224-
author = author_factory(bio=None)
225-
url = reverse("author-detail", args=[author.pk])
226-
response = client.get(url)
227-
assert response.status_code == 200
228-
content = response.json()
229-
assert "included" not in content
222+
def test_included_array_populated_when_related_data_exists(
223+
blog_factory, tagged_item_factory, client
224+
):
225+
blog = blog_factory()
226+
tag = tagged_item_factory(tag="django")
227+
blog.tags.add(tag)
228+
229+
response = client.get(
230+
reverse("blog-detail", kwargs={"pk": blog.pk}) + "?include=tags"
231+
)
232+
included = response.json()["included"]
233+
234+
assert included, "Expected included array to be populated"
235+
assert [x.get("type") for x in included] == [
236+
"taggedItems"
237+
], "Included types incorrect"
238+
assert included[0]["attributes"]["tag"] == "django"
239+
240+
241+
def test_included_array_present_via_jsonapimeta_defaults(
242+
single_entry, comment_factory, author_factory, client
243+
):
244+
author = author_factory()
245+
comment_factory(entry=single_entry, author=author)
246+
247+
response = client.get(reverse("entry-detail", kwargs={"pk": single_entry.pk}))
248+
249+
included = response.json()["included"]
250+
251+
assert included, "Expected included array due to JSONAPIMeta defaults"
252+
assert any(resource["type"] == "comments" for resource in included)

example/tests/test_filters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ def test_search_keywords(self):
530530
},
531531
"meta": {"bodyFormat": "text"},
532532
}
533-
]
533+
],
534+
"included": [],
534535
}
535536
assert response.json() == expected_result
536537

rest_framework_json_api/renderers.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,17 +652,13 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
652652
if not included_cache[obj_type]:
653653
del included_cache[obj_type]
654654

655-
if included_cache:
655+
if included_resources:
656656
render_data["included"] = list()
657657
for included_type in sorted(included_cache.keys()):
658658
for included_id in sorted(included_cache[included_type].keys()):
659659
render_data["included"].append(
660660
included_cache[included_type][included_id]
661661
)
662-
else:
663-
request = renderer_context.get("request")
664-
if request and "include" in request.query_params:
665-
render_data["included"] = []
666662

667663
if json_api_meta:
668664
render_data["meta"] = format_field_names(json_api_meta)

setup.cfg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ exclude =
2121
.eggs
2222
.tox,
2323
env,
24-
.venv,
25-
venv
24+
.venv
2625

2726
[isort]
2827
multi_line_output = 3

tox.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,3 @@ deps =
4848
commands =
4949
sphinx-build -W -b html -d docs/_build/doctrees docs docs/_build/html
5050

51-
52-

0 commit comments

Comments
 (0)