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 7abd764

Browse files
n2ygksliverc
authored andcommitted
remove disallowed PUT method. (#643)
1 parent 21600c6 commit 7abd764

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

‎CHANGELOG.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ any parts of the framework not mentioned in the documentation should generally b
2626
* Don't swallow `filter[]` params when there are several
2727
* Fix DeprecationWarning regarding collections.abc import in Python 3.7
2828
* Allow OPTIONS request to be used on RelationshipView
29-
* Avoid raising validation error for missing fields on a PATCH
30-
request for polymorphic serializers
29+
* Remove non-JSONAPI methods (PUT and TRACE) from ModelViewSet and RelationshipView.
30+
This fix might be a **BREAKING CHANGE** if your clients are incorrectly using PUT instead of PATCH.
31+
* Avoid validation error for missing fields on a PATCH request using polymorphic serializers
3132

3233
### Deprecated
3334

‎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
@@ -185,27 +185,42 @@ def test_polymorphism_relations_update(single_company, research_project_factory,
185185
"type": "researchProjects",
186186
"id": research_project.pk
187187
}
188-
response = client.put(reverse("company-detail", kwargs={'pk': single_company.pk}),
189-
data=content)
188+
response = client.patch(reverse("company-detail", kwargs={'pk': single_company.pk}),
189+
data=content)
190190
assert response.status_code == 200
191191
content = response.json()
192192
assert content["data"]["relationships"]["currentProject"]["data"]["type"] == "researchProjects"
193193
assert int(content["data"]["relationships"]["currentProject"]["data"]["id"]) == \
194194
research_project.pk
195195

196196

197-
def test_invalid_type_on_polymorphic_relation(single_company, research_project_factory, client):
197+
def test_polymorphism_relations_put_405(single_company, research_project_factory, client):
198198
response = client.get(reverse("company-detail", kwargs={'pk': single_company.pk}))
199199
content = response.json()
200200
assert content["data"]["relationships"]["currentProject"]["data"]["type"] == "artProjects"
201201

202202
research_project = research_project_factory()
203203
content["data"]["relationships"]["currentProject"]["data"] = {
204-
"type": "invalidProjects",
204+
"type": "researchProjects",
205205
"id": research_project.pk
206206
}
207207
response = client.put(reverse("company-detail", kwargs={'pk': single_company.pk}),
208208
data=content)
209+
assert response.status_code == 405
210+
211+
212+
def test_invalid_type_on_polymorphic_relation(single_company, research_project_factory, client):
213+
response = client.get(reverse("company-detail", kwargs={'pk': single_company.pk}))
214+
content = response.json()
215+
assert content["data"]["relationships"]["currentProject"]["data"]["type"] == "artProjects"
216+
217+
research_project = research_project_factory()
218+
content["data"]["relationships"]["currentProject"]["data"] = {
219+
"type": "invalidProjects",
220+
"id": research_project.pk
221+
}
222+
response = client.patch(reverse("company-detail", kwargs={'pk': single_company.pk}),
223+
data=content)
209224
assert response.status_code == 409
210225
content = response.json()
211226
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)

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