From 49be826c01ceb1ddd2635810e3ba4bc8176044d8 Mon Sep 17 00:00:00 2001 From: Oliver Sauder Date: Tue, 6 Jul 2021 22:29:06 +0400 Subject: [PATCH] Remove invalid validation of default included_resources --- CHANGELOG.md | 3 ++- example/serializers.py | 3 +++ example/tests/test_serializers.py | 20 +++++++++++++++++++- rest_framework_json_api/serializers.py | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 508da91f..48a5bd7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Note that in line with [Django REST Framework policy](http://www.django-rest-framework.org/topics/release-notes/), any parts of the framework not mentioned in the documentation should generally be considered private API, and may be subject to change. -## Unreleased +## [Unreleased] ### Fixed * Include `PreloadIncludesMixin` in `ReadOnlyModelViewSet` to enable the usage of [performance utilities](https://django-rest-framework-json-api.readthedocs.io/en/stable/usage.html#performance-improvements) on read only views (regression since 2.8.0) +* Remove invalid validation of default `included_resources` (regression since 4.2.0) ## [4.2.0] - 2021-05-12 diff --git a/example/serializers.py b/example/serializers.py index 64444e9e..43415243 100644 --- a/example/serializers.py +++ b/example/serializers.py @@ -321,6 +321,9 @@ class Meta: # fields = ('entry', 'body', 'author',) meta_fields = ("modified_days_ago",) + class JSONAPIMeta: + included_resources = ("writer",) + def get_modified_days_ago(self, obj): return (datetime.now() - obj.modified_at).days diff --git a/example/tests/test_serializers.py b/example/tests/test_serializers.py index 7550fd2c..9cfe6db9 100644 --- a/example/tests/test_serializers.py +++ b/example/tests/test_serializers.py @@ -198,7 +198,25 @@ def test_model_serializer_with_implicit_fields(self, comment, client): "meta": { "modifiedDaysAgo": (datetime.now() - comment.modified_at).days }, - } + }, + "included": [ + { + "attributes": { + "email": comment.author.email, + "name": comment.author.name, + }, + "id": str(comment.author.pk), + "relationships": { + "bio": { + "data": { + "id": str(comment.author.bio.pk), + "type": "authorBios", + } + } + }, + "type": "writers", + } + ], } response = client.get(reverse("comment-detail", kwargs={"pk": comment.pk})) diff --git a/rest_framework_json_api/serializers.py b/rest_framework_json_api/serializers.py index 5ca773d0..a73a5d47 100644 --- a/rest_framework_json_api/serializers.py +++ b/rest_framework_json_api/serializers.py @@ -137,7 +137,7 @@ def validate_path(serializer_class, field_path, path): validate_path(this_included_serializer, new_included_field_path, path) if request and view: - included_resources = get_included_resources(request, self) + included_resources = get_included_resources(request) for included_field_name in included_resources: included_field_path = included_field_name.split(".") if "related_field" in view.kwargs: