Skip to content

remove disallowed PUT method. #643

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

Merged
merged 2 commits into from
Jun 8, 2019

Conversation

n2ygk
Copy link
Contributor

@n2ygk n2ygk commented May 28, 2019

Fixes #614

Description of the Change

This removes the PUT method which is not specified in the JSONAPI specification. Rather, PATCH
is used for updates. PUT and PATCH were both implemented as a PATCH which is incorrect as a PUT is expected to be a complete replacement while PATCH is a partial replacement.

POTENTIAL BREAKING CHANGE: There were tests and documentation that used put that should have used patch and it is possible that clients may be using PUT.

The most visible outcome of removing PUT is that forthcoming DRF 3.10 generateschema would incorrectly add the put method to the openapi schema doc.

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • CHANGELOG.md updated (only for user relevant changes)
  • author name in AUTHORS

@n2ygk n2ygk requested a review from sliverc May 28, 2019 19:29
@codecov

This comment has been minimized.

@codecov

This comment has been minimized.

@n2ygk

This comment has been minimized.

@n2ygk

This comment has been minimized.

@n2ygk

This comment has been minimized.

@n2ygk

This comment has been minimized.

@n2ygk

This comment has been minimized.

@n2ygk

This comment has been minimized.

@n2ygk
Copy link
Contributor Author

n2ygk commented May 31, 2019

I'm going to reapply my changes against master. I must have messed up the merge.

No, the merge was OK. Somehow replacing DJA's ModelViewSet is causing this error here:

-class CompanyViewset(PreloadIncludesMixin, viewsets.ModelViewSet):
+class CompanyViewset(ModelViewSet):

leading to this failure:

ERROR    django.request:log.py:228 Internal Server Error: /companies/1
Traceback (most recent call last):
  File "/Users/alan/src/django-rest-framework-json-api/rest_framework_json_api/serializers.py", line 286, in get_polymorphic_serializer_for_instance
    return cls._poly_model_serializer_map[instance._meta.model]
KeyError: <class 'example.models.Project'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/alan/src/django-rest-framework-json-api/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/alan/src/django-rest-framework-json-api/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/alan/src/django-rest-framework-json-api/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/Users/alan/src/django-rest-framework-json-api/env/lib/python3.7/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/Users/alan/src/django-rest-framework-json-api/env/lib/python3.7/site-packages/rest_framework/response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/Users/alan/src/django-rest-framework-json-api/rest_framework_json_api/renderers.py", line 631, in render
    fields, serializer_data, resource_instance, included_resources, included_cache
  File "/Users/alan/src/django-rest-framework-json-api/rest_framework_json_api/renderers.py", line 393, in extract_included
    serializer_data = field.data
  File "/Users/alan/src/django-rest-framework-json-api/env/lib/python3.7/site-packages/rest_framework/serializers.py", line 563, in data
    ret = super(Serializer, self).data
  File "/Users/alan/src/django-rest-framework-json-api/env/lib/python3.7/site-packages/rest_framework/serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "/Users/alan/src/django-rest-framework-json-api/rest_framework_json_api/serializers.py", line 339, in to_representation
    serializer_class = self.get_polymorphic_serializer_for_instance(instance)
  File "/Users/alan/src/django-rest-framework-json-api/rest_framework_json_api/serializers.py", line 290, in get_polymorphic_serializer_for_instance
    instance._meta.model.__name__))
NotImplementedError: No polymorphic serializer has been found for model Project
========================================================= 1 failed, 198 passed in 5.79 seconds =========================================================

I'll take a deeper dive into DRF vs DJA ModelViewSet.

@n2ygk
Copy link
Contributor Author

n2ygk commented May 31, 2019

@Anton-Shutik: the problem is caused by the combination of AutoPreloadMixin and PreloadIncludesMixin. Commenting out AutoPreloadMixin fixes it. I really don't know what these mixins are supposed to do. Can you take a look please?

diff --git a/example/views.py b/example/views.py
index eda29f8..7ebf9a8 100644
--- a/example/views.py
+++ b/example/views.py
@@ -12,7 +12,8 @@ from rest_framework_json_api.django_filters import DjangoFilterBackend
 from rest_framework_json_api.filters import OrderingFilter, QueryParameterValidationFilter
 from rest_framework_json_api.pagination import JsonApiPageNumberPagination
 from rest_framework_json_api.utils import format_drf_errors
-from rest_framework_json_api.views import ModelViewSet, PreloadIncludesMixin, RelationshipView
+from rest_framework_json_api.views import ModelViewSet, PreloadIncludesMixin, RelationshipView, AutoPreloadMixin, \
+    RelatedMixin
 
 from example.models import Author, Blog, Comment, Company, Entry, Project, ProjectType
 from example.serializers import (
@@ -200,7 +201,16 @@ class CommentViewSet(ModelViewSet):
         return super(CommentViewSet, self).get_queryset()
 
 
-class CompanyViewset(PreloadIncludesMixin, viewsets.ModelViewSet):
+class TestModelViewSet(
+    # AutoPreloadMixin,
+    PreloadIncludesMixin,
+    RelatedMixin,
+    viewsets.ModelViewSet):
+    http_method_names = ['get', 'post', 'patch', 'delete', 'head', 'options']
+
+
+# class CompanyViewset(PreloadIncludesMixin, viewsets.ModelViewSet):
+class CompanyViewset(TestModelViewSet):
     queryset = Company.objects.all()
     serializer_class = CompanySerializer
     prefetch_for_includes = {

@n2ygk n2ygk self-assigned this Jun 7, 2019
@n2ygk n2ygk force-pushed the issue-614/remove_put branch from 1212c36 to 8316af2 Compare June 7, 2019 16:32
@n2ygk n2ygk force-pushed the issue-614/remove_put branch from 6804b57 to 70f9b41 Compare June 7, 2019 16:40
@n2ygk n2ygk requested a review from sliverc June 7, 2019 16:43
@sliverc
Copy link
Member

sliverc commented Jun 8, 2019

No clue why codecov is not updating, happens to all PRs. Merging this now and need to observe whether this might get fixed automatically.

@sliverc sliverc merged commit 7abd764 into django-json-api:master Jun 8, 2019
@n2ygk n2ygk deleted the issue-614/remove_put branch June 9, 2019 17:18
kolomenkin added a commit to private-forks/django-rest-framework-json-api that referenced this pull request Jun 28, 2020
…-json-api#797)

Currently if you try to use `POST` for action in `ReadOnlyModelViewSet` you will get problems:
- with DRF UI you will loose data input forms
- `drf_yasg` package will not generate OpenAPI specifications for such actions

This behavior was added to `django-rest-framework-json-api` in version 2.8.0 by mistake:

Commit: 7abd764
Subject: remove disallowed PUT method. (django-json-api#643)
kolomenkin added a commit to private-forks/django-rest-framework-json-api that referenced this pull request Jun 28, 2020
…-json-api#797)

Currently if you try to use `POST` for action in `ReadOnlyModelViewSet` you will get problems:
- with DRF UI you will loose data input forms
- `drf_yasg` package will not generate OpenAPI specifications for such actions

This behavior was added to `django-rest-framework-json-api` in version 2.8.0 by mistake:

Commit: 7abd764
Subject: remove disallowed PUT method. (django-json-api#643)
kolomenkin added a commit to private-forks/django-rest-framework-json-api that referenced this pull request Jun 28, 2020
…-json-api#797)

Currently if you try to use `POST` for action in `ReadOnlyModelViewSet` you will get problems:
- with DRF UI you will loose data input forms
- `drf_yasg` package will not generate OpenAPI specifications for such actions

This behavior was added to `django-rest-framework-json-api` in version 2.8.0 by mistake:

Commit: 7abd764
Subject: remove disallowed PUT method. (django-json-api#643)
kolomenkin added a commit to private-forks/django-rest-framework-json-api that referenced this pull request Jun 28, 2020
…-json-api#797)

Currently if you try to use `POST` for action in `ReadOnlyModelViewSet` you will get problems:
- with DRF UI you will loose data input forms
- `drf_yasg` package will not generate OpenAPI specifications for such actions

This behavior was added to `django-rest-framework-json-api` in version 2.8.0 by mistake:

Commit: 7abd764
Subject: remove disallowed PUT method. (django-json-api#643)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

remove ModelViewSet PUT/update() method?
2 participants