Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 06c3ef4

Browse files
authored
Deprecated usage of type field name (#993)
This support was added in #376 but only for non polymorphic fields. However as per specification [0] `type` must not be a field name and therefore must be forbidden in DJA as well. Some dependents might depend on being allowed to have a field name `type` so deprecating it now and remove it in next major version. [0] https://jsonapi.org/format/#document-resource-object-fields
1 parent e17ea57 commit 06c3ef4

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ any parts of the framework not mentioned in the documentation should generally b
2424
### Deprecated
2525

2626
* Deprecated `get_included_serializers(serializer)` function under `rest_framework_json_api.utils`. Use `serializer.included_serializers` instead.
27+
* Deprecated support for field name `type` as it may not be used according to the [JSON:API spec](https://jsonapi.org/format/#document-resource-object-fields).
2728

2829
## [4.2.1] - 2021年07月06日
2930

‎example/tests/test_model_viewsets.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,21 @@ def test_patch_allow_field_type(author, author_type_factory, client):
223223
"""
224224
Verify that type field may be updated.
225225
"""
226-
author_type = author_type_factory()
227-
url = reverse("author-detail", args=[author.id])
228-
229-
data = {
230-
"data": {
231-
"id": author.id,
232-
"type": "authors",
233-
"relationships": {"data": {"id": author_type.id, "type": "author-type"}},
226+
# TODO remove in next major version 5.0.0 see serializers.ReservedFieldNamesMixin
227+
with pytest.deprecated_call():
228+
author_type = author_type_factory()
229+
url = reverse("author-detail", args=[author.id])
230+
231+
data = {
232+
"data": {
233+
"id": author.id,
234+
"type": "authors",
235+
"relationships": {
236+
"data": {"id": author_type.id, "type": "author-type"}
237+
},
238+
}
234239
}
235-
}
236240

237-
response = client.patch(url, data=data)
241+
response = client.patch(url, data=data)
238242

239-
assert response.status_code == 200
243+
assert response.status_code == 200

‎rest_framework_json_api/parsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def parse(self, stream, media_type=None, parser_context=None):
162162
# Construct the return data
163163
serializer_class = getattr(view, "serializer_class", None)
164164
parsed_data = {"id": data.get("id")} if "id" in data else {}
165-
# `type` field needs to be allowed in none polymorphic serializers
165+
# TODO remove in next major version 5.0.0 see serializers.ReservedFieldNamesMixin
166166
if serializer_class is not None:
167167
if issubclass(serializer_class, serializers.PolymorphicModelSerializer):
168168
parsed_data["type"] = data.get("type")

‎rest_framework_json_api/serializers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
from collections import OrderedDict
23
from collections.abc import Mapping
34

@@ -171,6 +172,18 @@ def get_fields(self):
171172
f"{', '.join(sorted(found_reserved_field_names))}"
172173
)
173174

175+
if "type" in fields:
176+
# see https://jsonapi.org/format/#document-resource-object-fields
177+
warnings.warn(
178+
DeprecationWarning(
179+
f"Field name 'type' found in serializer class "
180+
f"{self.__class__.__module__}.{self.__class__.__qualname__} "
181+
f"which is not allowed according to the JSON:API spec and "
182+
f"won't be supported anymore in the next major DJA release. "
183+
f"Rename 'type' field to something else. "
184+
)
185+
)
186+
174187
return fields
175188

176189

‎setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ filterwarnings =
6060
error::PendingDeprecationWarning
6161
# Django Debug Toolbar currently (2021年04月07日) specifies default_app_config which is deprecated in Django 3.2:
6262
ignore:'debug_toolbar' defines default_app_config = 'debug_toolbar.apps.DebugToolbarConfig'. Django now detects this configuration automatically. You can remove default_app_config.:PendingDeprecationWarning
63+
# TODO remove in next major version of DJA 5.0.0
64+
# this deprecation warning filter needs to be added as AuthorSerializer is used in
65+
# too many tests which introduced the type field name in tests
66+
ignore:Field name 'type'
6367
testpaths =
6468
example
6569
tests

‎tests/test_serializers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@ class ReservedFieldNamesSerializer(serializers.Serializer):
5050
"ReservedFieldNamesSerializer uses following reserved field name(s) which is "
5151
"not allowed: meta, results"
5252
)
53+
54+
55+
def test_serializer_fields_deprecated_field_name_type():
56+
with pytest.deprecated_call():
57+
58+
class TypeFieldNameSerializer(serializers.Serializer):
59+
type = serializers.CharField()
60+
61+
TypeFieldNameSerializer().fields

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /