Skip to content

Commit 70f9b41

Browse files
committedJun 7, 2019
remove disallowed PUT method.
1 parent 32a9d11 commit 70f9b41

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed
 

‎CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ any parts of the framework not mentioned in the documentation should generally b
2525
* Avoid exception when trying to include skipped relationship
2626
* Don't swallow `filter[]` params when there are several
2727
* Fix DeprecationWarning regarding collections.abc import in Python 3.7
28-
* Allow OPTIONS request to be used on RelationshipView
28+
* Allow OPTIONS request to be used on RelationshipView.
29+
* Remove non-JSONAPI methods (PUT and TRACE) from ModelViewSet and RelationshipView. This is a **BREAKING CHANGE** if
30+
your clients are incorrectly using PUT instead of PATCH.
2931

3032
### Deprecated
3133

‎docs/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ Possible values:
322322
* underscore
323323

324324
Note: due to the way the inflector works `address_1` can camelize to `address1`
325-
on output but it cannot convert `address1` back to `address_1` on POST or PUT. Keep
325+
on output but it cannot convert `address1` back to `address_1` on POST or PATCH. Keep
326326
this in mind when naming fields with numbers in them.
327327

328328

‎example/tests/integration/test_polymorphism.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,42 @@ def test_polymorphism_relations_update(single_company, research_project_factory,
168168
"type": "researchProjects",
169169
"id": research_project.pk
170170
}
171-
response = client.put(reverse("company-detail", kwargs={'pk': single_company.pk}),
172-
data=content)
171+
response = client.patch(reverse("company-detail", kwargs={'pk': single_company.pk}),
172+
data=content)
173173
assert response.status_code == 200
174174
content = response.json()
175175
assert content["data"]["relationships"]["currentProject"]["data"]["type"] == "researchProjects"
176176
assert int(content["data"]["relationships"]["currentProject"]["data"]["id"]) == \
177177
research_project.pk
178178

179179

180-
def test_invalid_type_on_polymorphic_relation(single_company, research_project_factory, client):
180+
def test_polymorphism_relations_put_405(single_company, research_project_factory, client):
181181
response = client.get(reverse("company-detail", kwargs={'pk': single_company.pk}))
182182
content = response.json()
183183
assert content["data"]["relationships"]["currentProject"]["data"]["type"] == "artProjects"
184184

185185
research_project = research_project_factory()
186186
content["data"]["relationships"]["currentProject"]["data"] = {
187-
"type": "invalidProjects",
187+
"type": "researchProjects",
188188
"id": research_project.pk
189189
}
190190
response = client.put(reverse("company-detail", kwargs={'pk': single_company.pk}),
191191
data=content)
192+
assert response.status_code == 405
193+
194+
195+
def test_invalid_type_on_polymorphic_relation(single_company, research_project_factory, client):
196+
response = client.get(reverse("company-detail", kwargs={'pk': single_company.pk}))
197+
content = response.json()
198+
assert content["data"]["relationships"]["currentProject"]["data"]["type"] == "artProjects"
199+
200+
research_project = research_project_factory()
201+
content["data"]["relationships"]["currentProject"]["data"] = {
202+
"type": "invalidProjects",
203+
"id": research_project.pk
204+
}
205+
response = client.patch(reverse("company-detail", kwargs={'pk': single_company.pk}),
206+
data=content)
192207
assert response.status_code == 409
193208
content = response.json()
194209
assert len(content["errors"]) == 1

‎example/views.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import rest_framework.parsers
33
import rest_framework.renderers
44
from django_filters import rest_framework as filters
5-
from rest_framework import viewsets
65
from rest_framework.filters import SearchFilter
76

87
import rest_framework_json_api.metadata
@@ -42,7 +41,7 @@ def get_object(self):
4241
return super(BlogViewSet, self).get_object()
4342

4443

45-
class DRFBlogViewSet(viewsets.ModelViewSet):
44+
class DRFBlogViewSet(ModelViewSet):
4645
queryset = Blog.objects.all()
4746
serializer_class = BlogDRFSerializer
4847
lookup_url_kwarg = 'entry_pk'
@@ -105,7 +104,7 @@ def get_object(self):
105104
return super(EntryViewSet, self).get_object()
106105

107106

108-
class DRFEntryViewSet(viewsets.ModelViewSet):
107+
class DRFEntryViewSet(ModelViewSet):
109108
queryset = Entry.objects.all()
110109
serializer_class = EntryDRFSerializers
111110
lookup_url_kwarg = 'entry_pk'

‎rest_framework_json_api/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,21 @@ class ModelViewSet(AutoPrefetchMixin,
243243
PreloadIncludesMixin,
244244
RelatedMixin,
245245
viewsets.ModelViewSet):
246-
pass
246+
http_method_names = ['get', 'post', 'patch', 'delete', 'head', 'options']
247247

248248

249249
class ReadOnlyModelViewSet(AutoPrefetchMixin,
250250
RelatedMixin,
251251
viewsets.ReadOnlyModelViewSet):
252-
pass
252+
http_method_names = ['get', 'head', 'options']
253253

254254

255255
class RelationshipView(generics.GenericAPIView):
256256
serializer_class = ResourceIdentifierObjectSerializer
257257
self_link_view_name = None
258258
related_link_view_name = None
259259
field_name_mapping = {}
260+
http_method_names = ['get', 'post', 'patch', 'delete', 'head', 'options']
260261

261262
def get_serializer_class(self):
262263
if getattr(self, 'action', False) is None:

0 commit comments

Comments
 (0)