Skip to content

Commit

Permalink
Removed all deprecations (#1040)
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Crosswell <[email protected]>
  • Loading branch information
sliverc and n2ygk authored Dec 30, 2021
1 parent 8cd79ae commit aa33959
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 158 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ any parts of the framework not mentioned in the documentation should generally b
* Removed support for Django 3.0.
* Removed support for Django 3.1.
* Removed support for Python 3.6.
* Removed obsolete method `utils.get_included_serializers`.
* Removed optional `format_type` argument of `utils.format_link_segment`.
* Removed `format_type`s default argument of `utils.format_value`. `format_type` is now required.

## [4.3.0] - 2021-12-10

Expand Down
2 changes: 1 addition & 1 deletion example/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Meta:
email = factory.LazyAttribute(lambda x: faker.email())

bio = factory.RelatedFactory("example.factories.AuthorBioFactory", "author")
type = factory.SubFactory(AuthorTypeFactory)
author_type = factory.SubFactory(AuthorTypeFactory)


class AuthorBioFactory(factory.django.DjangoModelFactory):
Expand Down
4 changes: 2 additions & 2 deletions example/fixtures/blogentry.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"modified_at": "2016-05-02T10:09:48.277",
"name": "Alice",
"email": "[email protected]",
"type": null
"author_type": null
}
},
{
Expand All @@ -88,7 +88,7 @@
"modified_at": "2016-05-02T10:09:57.133",
"name": "Bob",
"email": "[email protected]",
"type": null
"author_type": null
}
},
{
Expand Down
31 changes: 31 additions & 0 deletions example/migrations/0011_rename_type_author_author_type_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.0 on 2021-12-29 13:07

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("contenttypes", "0002_remove_content_type_name"),
("example", "0010_auto_20210714_0809"),
]

operations = [
migrations.RenameField(
model_name="author",
old_name="type",
new_name="author_type",
),
migrations.AlterField(
model_name="project",
name="polymorphic_ctype",
field=models.ForeignKey(
editable=False,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="polymorphic_%(app_label)s.%(class)s_set+",
to="contenttypes.contenttype",
),
),
]
2 changes: 1 addition & 1 deletion example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Meta:
class Author(BaseModel):
name = models.CharField(max_length=50)
email = models.EmailField()
type = models.ForeignKey(AuthorType, null=True, on_delete=models.CASCADE)
author_type = models.ForeignKey(AuthorType, null=True, on_delete=models.CASCADE)

def __str__(self):
return self.name
Expand Down
9 changes: 6 additions & 3 deletions example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,13 @@ class AuthorSerializer(serializers.ModelSerializer):
help_text="help for defaults",
)
initials = serializers.SerializerMethodField()
included_serializers = {"bio": AuthorBioSerializer, "type": AuthorTypeSerializer}
included_serializers = {
"bio": AuthorBioSerializer,
"author_type": AuthorTypeSerializer,
}
related_serializers = {
"bio": "example.serializers.AuthorBioSerializer",
"type": "example.serializers.AuthorTypeSerializer",
"author_type": "example.serializers.AuthorTypeSerializer",
"comments": "example.serializers.CommentSerializer",
"entries": "example.serializers.EntrySerializer",
"first_entry": "example.serializers.EntrySerializer",
Expand All @@ -270,7 +273,7 @@ class Meta:
"entries",
"comments",
"first_entry",
"type",
"author_type",
"secrets",
"defaults",
"initials",
Expand Down
12 changes: 6 additions & 6 deletions example/tests/__snapshots__/test_openapi.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
},
"relationships": {
"properties": {
"authorType": {
"$ref": "#/components/schemas/reltoone"
},
"bio": {
"$ref": "#/components/schemas/reltoone"
},
Expand All @@ -135,9 +138,6 @@
},
"firstEntry": {
"$ref": "#/components/schemas/reltoone"
},
"type": {
"$ref": "#/components/schemas/reltoone"
}
},
"type": "object"
Expand Down Expand Up @@ -532,6 +532,9 @@
},
"relationships": {
"properties": {
"authorType": {
"$ref": "#/components/schemas/reltoone"
},
"bio": {
"$ref": "#/components/schemas/reltoone"
},
Expand All @@ -543,9 +546,6 @@
},
"firstEntry": {
"$ref": "#/components/schemas/reltoone"
},
"type": {
"$ref": "#/components/schemas/reltoone"
}
},
"type": "object"
Expand Down
2 changes: 1 addition & 1 deletion example/tests/test_format_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_options_format_field_names(db, client):
"bio",
"entries",
"firstEntry",
"type",
"authorType",
"comments",
"secrets",
"defaults",
Expand Down
26 changes: 0 additions & 26 deletions example/tests/test_model_viewsets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from django.contrib.auth import get_user_model
from django.test import override_settings
from django.urls import reverse
Expand Down Expand Up @@ -216,28 +215,3 @@ def test_404_error_pointer(self):
response = self.client.get(not_found_url)
assert 404 == response.status_code
assert errors == response.json()


@pytest.mark.django_db
def test_patch_allow_field_type(author, author_type_factory, client):
"""
Verify that type field may be updated.
"""
# TODO remove in next major version 5.0.0 see serializers.ReservedFieldNamesMixin
with pytest.deprecated_call():
author_type = author_type_factory()
url = reverse("author-detail", args=[author.id])

data = {
"data": {
"id": author.id,
"type": "authors",
"relationships": {
"data": {"id": author_type.id, "type": "author-type"}
},
}
}

response = client.patch(url, data=data)

assert response.status_code == 200
8 changes: 4 additions & 4 deletions example/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def test_get_related_serializer_class_many(self):
self.assertEqual(got, EntrySerializer)

def test_get_serializer_comes_from_included_serializers(self):
kwargs = {"pk": self.author.id, "related_field": "type"}
kwargs = {"pk": self.author.id, "related_field": "author_type"}
view = self._get_view(kwargs)
related_serializers = view.get_serializer_class().related_serializers
delattr(view.get_serializer_class(), "related_serializers")
Expand Down Expand Up @@ -470,14 +470,14 @@ def test_retrieve_related_single_reverse_lookup(self):
def test_retrieve_related_single(self):
url = reverse(
"author-related",
kwargs={"pk": self.author.type.pk, "related_field": "type"},
kwargs={"pk": self.author.author_type.pk, "related_field": "author_type"},
)
resp = self.client.get(url)
expected = {
"data": {
"type": "authorTypes",
"id": str(self.author.type.id),
"attributes": {"name": str(self.author.type.name)},
"id": str(self.author.author_type.id),
"attributes": {"name": str(self.author.author_type.name)},
}
}
self.assertEqual(resp.status_code, 200)
Expand Down
8 changes: 2 additions & 6 deletions rest_framework_json_api/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rest_framework import parsers
from rest_framework.exceptions import ParseError

from rest_framework_json_api import exceptions, renderers, serializers
from rest_framework_json_api import exceptions, renderers
from rest_framework_json_api.utils import get_resource_name, undo_format_field_names


Expand Down Expand Up @@ -160,12 +160,8 @@ def parse(self, stream, media_type=None, parser_context=None):
)

# Construct the return data
serializer_class = getattr(view, "serializer_class", None)
parsed_data = {"id": data.get("id")} if "id" in data else {}
# TODO remove in next major version 5.0.0 see serializers.ReservedFieldNamesMixin
if serializer_class is not None:
if issubclass(serializer_class, serializers.PolymorphicModelSerializer):
parsed_data["type"] = data.get("type")
parsed_data["type"] = data.get("type")
parsed_data.update(self.parse_attributes(data))
parsed_data.update(self.parse_relationships(data))
parsed_data.update(self.parse_metadata(result))
Expand Down
15 changes: 1 addition & 14 deletions rest_framework_json_api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import warnings
from collections import OrderedDict
from collections.abc import Mapping

Expand Down Expand Up @@ -157,7 +156,7 @@ def validate_path(serializer_class, field_path, path):
class ReservedFieldNamesMixin:
"""Ensures that reserved field names are not used and an error raised instead."""

_reserved_field_names = {"meta", "results"}
_reserved_field_names = {"meta", "results", "type"}

def get_fields(self):
fields = super().get_fields()
Expand All @@ -171,18 +170,6 @@ def get_fields(self):
f"{', '.join(sorted(found_reserved_field_names))}"
)

if "type" in fields:
# see https://jsonapi.org/format/#document-resource-object-fields
warnings.warn(
DeprecationWarning(
f"Field name 'type' found in serializer class "
f"{self.__class__.__module__}.{self.__class__.__qualname__} "
f"which is not allowed according to the JSON:API spec and "
f"won't be supported anymore in the next major DJA release. "
f"Rename 'type' field to something else. "
)
)

return fields


Expand Down
35 changes: 3 additions & 32 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import inspect
import operator
import warnings
from collections import OrderedDict

import inflection
Expand Down Expand Up @@ -147,23 +146,14 @@ def undo_format_field_name(field_name):
return field_name


def format_link_segment(value, format_type=None):
def format_link_segment(value):
"""
Takes a string value and returns it with formatted keys as set in `format_type`
or `JSON_API_FORMAT_RELATED_LINKS`.
:format_type: Either 'dasherize', 'camelize', 'capitalize' or 'underscore'
"""
if format_type is None:
format_type = json_api_settings.FORMAT_RELATED_LINKS
else:
warnings.warn(
DeprecationWarning(
"Using `format_type` argument is deprecated."
"Use `format_value` instead."
)
)

format_type = json_api_settings.FORMAT_RELATED_LINKS
return format_value(value, format_type)


Expand All @@ -179,15 +169,7 @@ def undo_format_link_segment(value):
return value


def format_value(value, format_type=None):
if format_type is None:
warnings.warn(
DeprecationWarning(
"Using `format_value` without passing on `format_type` argument is deprecated."
"Use `format_field_name` instead."
)
)
format_type = json_api_settings.FORMAT_FIELD_NAMES
def format_value(value, format_type):
if format_type == "dasherize":
# inflection can't dasherize camelCase
value = inflection.underscore(value)
Expand Down Expand Up @@ -342,17 +324,6 @@ def get_default_included_resources_from_serializer(serializer):
return list(getattr(meta, "included_resources", []))


def get_included_serializers(serializer):
warnings.warn(
DeprecationWarning(
"Using of `get_included_serializers(serializer)` function is deprecated."
"Use `serializer.included_serializers` instead."
)
)

return getattr(serializer, "included_serializers", dict())


def get_relation_instance(resource_instance, source, serializer):
try:
relation_instance = operator.attrgetter(source)(resource_instance)
Expand Down
4 changes: 0 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ DJANGO_SETTINGS_MODULE=example.settings.test
filterwarnings =
error::DeprecationWarning
error::PendingDeprecationWarning
# TODO remove in next major version of DJA 5.0.0
# this deprecation warning filter needs to be added as AuthorSerializer is used in
# too many tests which introduced the type field name in tests
ignore:Field name 'type'
testpaths =
example
tests
3 changes: 2 additions & 1 deletion tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_parse_formats_field_names(
result = parse(data, parser_context)
assert result == {
"id": "123",
"type": "BasicModel",
"test_attribute": "test-value",
"test_relationship": {"id": "123", "type": "TestRelationship"},
}
Expand All @@ -85,7 +86,7 @@ def test_parse_with_default_arguments(self, parse):
},
}
result = parse(data, None)
assert result == {}
assert result == {"type": "BasicModel"}

def test_parse_preserves_json_value_field_names(
self, settings, parse, parser_context
Expand Down
9 changes: 0 additions & 9 deletions tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,3 @@ class ReservedFieldNamesSerializer(serializers.Serializer):
"ReservedFieldNamesSerializer uses following reserved field name(s) which is "
"not allowed: meta, results"
)


def test_serializer_fields_deprecated_field_name_type():
with pytest.deprecated_call():

class TypeFieldNameSerializer(serializers.Serializer):
type = serializers.CharField()

TypeFieldNameSerializer().fields
Loading

0 comments on commit aa33959

Please sign in to comment.