The API generator calls into the serializer to get the list of fields for POST
requests, but it does not fill in the request. This causes a TypeError when
accessing the Serializer context.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
api/rest.py | 7 ++++++-
tests/test_rest.py | 12 +++++++++++-
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/api/rest.py b/api/rest.py
index 61c0103..9e280d4 100644
--- a/api/rest.py
+++ b/api/rest.py
@@ -300,7 +300,12 @@ class MessageSerializer(BaseMessageSerializer):
def get_fields(self):
fields = super(MessageSerializer, self).get_fields()
- request = self.context['request']
+ try:
+ # When called from the CoreAPI schema generator, there is no context defined?
+ request = self.context['request']
+ except TypeError:
+ request = None
+
dispatch_module_hook("rest_message_fields_hook", request=request,
fields=fields)
return fields
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 6d86053..6a9c332 100755
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -10,6 +10,8 @@
import sys
import os
+import json
+import unittest
from django.contrib.auth.models import User
@@ -18,7 +20,11 @@ from patchewtest import PatchewTestCase, main
from api.models import Message
from api.rest import AddressSerializer
from collections import OrderedDict
-import json
+
+try:
+ import coreapi
+except ImportError:
+ coreapi = None
class RestTest(PatchewTestCase):
def setUp(self):
@@ -325,5 +331,9 @@ class RestTest(PatchewTestCase):
self.assertEqual(resp.data['results'][3]['resource_uri'], self.PROJECT_BASE + 'messages/e0858c00-ccb6-e533-ee3e-9ba84ca45a7b@redhat.com/')
self.assertEqual(resp.data['results'][3]['subject'], 'Re: [Qemu-devel] [PATCH v4 0/2] Report format specific info for LUKS block driver')
+ def test_schema(self):
+ resp = self.api_client.get(self.REST_BASE + 'schema/')
+ self.assertEqual(resp.status_code, 200)
+
if __name__ == '__main__':
main()
--
2.17.0
_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel