From e8c01ecdabe2abda48dd0cf298d4b6c743574449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= Date: Tue, 23 Sep 2014 21:12:58 -0400 Subject: [PATCH] Correctly propagate cloned_request for OPTIONS Update to fix pending changes in #1507 --- rest_framework/generics.py | 7 +++++-- tests/test_generics.py | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index a6f686571..a62da00b3 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -398,10 +398,11 @@ class GenericAPIView(views.APIView): if method not in self.allowed_methods: continue - cloned_request = clone_request(request, method) + original_request = self.request + self.request = clone_request(request, method) try: # Test global permissions - self.check_permissions(cloned_request) + self.check_permissions(self.request) # Test object permissions if method == 'PUT': try: @@ -419,6 +420,8 @@ class GenericAPIView(views.APIView): # appropriate metadata about the fields that should be supplied. serializer = self.get_serializer() actions[method] = serializer.metadata() + finally: + self.request = original_request if actions: ret['actions'] = actions diff --git a/tests/test_generics.py b/tests/test_generics.py index e9f5bebdd..dd3bab186 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -681,3 +681,42 @@ class TestFilterBackendAppliedToViews(TestCase): response = view(request).render() self.assertContains(response, 'field_b') self.assertNotContains(response, 'field_a') + + def test_options_with_dynamic_serializer(self): + """ + Ensure that OPTIONS returns correct POST json schema: + DynamicSerializer with single field 'field_b' + """ + request = factory.options('/') + view = DynamicSerializerView.as_view() + + with self.assertNumQueries(0): + response = view(request).render() + + expected = { + 'name': 'Dynamic Serializer', + 'description': '', + 'renders': [ + 'text/html', + 'application/json' + ], + 'parses': [ + 'application/json', + 'application/x-www-form-urlencoded', + 'multipart/form-data' + ], + 'actions': { + 'POST': { + 'field_b': { + 'type': u'string', + 'required': True, + 'read_only': False, + 'label': u'field b', + 'max_length': 100 + } + } + } + } + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data, expected)