8000 Merge pull request #1076 from edmondwong/master · lalkaka/django-rest-framework@196a895 · GitHub
[go: up one dir, main page]

Skip to content

Commit 196a895

Browse files
committed
Merge pull request encode#1076 from edmondwong/master
Allow OPTIONS to retrieve PUT field metadata on empty objects
2 parents 551fe92 + 6e7e4fc commit 196a895

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

rest_framework/generics.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,15 @@ def metadata(self, request):
356356
self.check_permissions(cloned_request)
357357
# Test object permissions
358358
if method == 'PUT':
359-
self.get_object()
360-
except (exceptions.APIException, PermissionDenied, Http404):
359+
try:
360+
self.get_object()
361+
except Http404:
362+
# Http404 should be acceptable and the serializer
363+
# metadata should be populated. Except this so the
364+
# outer "else" clause of the try-except-else block
365+
# will be executed.
366+
pass
367+
except (exceptions.APIException, PermissionDenied):
361368
pass
362369
else:
363370
# If user has appropriate permissions for the view, include

rest_framework/tests/test_generics.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,48 @@ def test_options_instance_view(self):
272272
self.assertEqual(response.status_code, status.HTTP_200_OK)
273273
self.assertEqual(response.data, expected)
274274

275+
def test_options_before_instance_create(self):
276+
"""
277+
OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata
278+
before the instance has been created
279+
"""
280+
request = factory.options('/999')
281+
with self.assertNumQueries(1):
282+
response = self.view(request, pk=999).render()
283+
expected = {
284+
'parses': [
285+
'application/json',
286+
'application/x-www-form-urlencoded',
287+
'multipart/form-data'
288+
],
289+
'renders': [
290+
'application/json',
291+
'text/html'
292+
],
293+
'name': 'Instance',
294+
'description': 'Example description for OPTIONS.',
295+
'actions': {
296+
'PUT': {
297+
'text': {
298+
'max_length': 100,
299+
'read_only': False,
300+
'required': True,
301+
'type': 'string',
302+
'label': 'Text comes here',
303+
'help_text': 'Text description.'
304+
},
305+
'id': {
306+
'read_only': True,
307+
'required': False,
308+
'type': 'integer',
309+
'label': 'ID',
310+
},
311+
}
312+
}
313+
}
314+
self.assertEqual(response.status_code, status.HTTP_200_OK)
315+
self.assertEqual(response.data, expected)
316+
275317
def test_get_instance_view_incorrect_arg(self):
276318
"""
277319
GET requests with an incorrect pk type, should raise 404, not 500.

0 commit comments

Comments
 (0)
0