Skip to content

Commit e8c01ec

Browse files
committed
Correctly propagate cloned_request for OPTIONS
Update to fix pending changes in encode#1507
1 parent 8495cd8 commit e8c01ec

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

rest_framework/generics.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,11 @@ def metadata(self, request):
398398
if method not in self.allowed_methods:
399399
continue
400400

401-
cloned_request = clone_request(request, method)
401+
original_request = self.request
402+
self.request = clone_request(request, method)
402403
try:
403404
# Test global permissions
404-
self.check_permissions(cloned_request)
405+
self.check_permissions(self.request)
405406
# Test object permissions
406407
if method == 'PUT':
407408
try:
@@ -419,6 +420,8 @@ def metadata(self, request):
419420
# appropriate metadata about the fields that should be supplied.
420421
serializer = self.get_serializer()
421422
actions[method] = serializer.metadata()
423+
finally:
424+
self.request = original_request
422425

423426
if actions:
424427
ret['actions'] = actions

tests/test_generics.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,3 +681,42 @@ def test_dynamic_serializer_form_in_browsable_api(self):
681681
response = view(request).render()
682682
self.assertContains(response, 'field_b')
683683
self.assertNotContains(response, 'field_a')
684+
685+
def test_options_with_dynamic_serializer(self):
686+
"""
687+
Ensure that OPTIONS returns correct POST json schema:
688+
DynamicSerializer with single field 'field_b'
689+
"""
690+
request = factory.options('/')
691+
view = DynamicSerializerView.as_view()
692+
693+
with self.assertNumQueries(0):
694+
response = view(request).render()
695+
696+
expected = {
697+
'name': 'Dynamic Serializer',
698+
'description': '',
699+
'renders': [
700+
'text/html',
701+
'application/json'
702+
],
703+
'parses': [
704+
'application/json',
705+
'application/x-www-form-urlencoded',
706+
'multipart/form-data'
707+
],
708+
'actions': {
709+
'POST': {
710+
'field_b': {
711+
'type': u'string',
712+
'required': True,
713+
'read_only': False,
714+
'label': u'field b',
715+
'max_length': 100
716+
}
717+
}
718+
}
719+
}
720+
721+
self.assertEqual(response.status_code, status.HTTP_200_OK)
722+
self.assertEqual(response.data, expected)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy