|
2 | 2 | from django.conf.urls import url |
3 | 3 | from django.test import RequestFactory, TestCase, override_settings |
4 | 4 |
|
5 | | -from rest_framework import filters, pagination |
| 5 | +from rest_framework import filters, generics, pagination, serializers |
6 | 6 | from rest_framework.compat import uritemplate |
7 | 7 | from rest_framework.request import Request |
8 | 8 | from rest_framework.schemas.generators import OpenAPISchemaGenerator |
@@ -84,6 +84,52 @@ def test_path_with_id_parameter(self): |
84 | 84 | }, |
85 | 85 | }] |
86 | 86 |
|
| 87 | + def test_request_body(self): |
| 88 | + path = '/' |
| 89 | + method = 'POST' |
| 90 | + |
| 91 | + class Serializer(serializers.Serializer): |
| 92 | + text = serializers.CharField() |
| 93 | + read_only = serializers.CharField(read_only=True) |
| 94 | + |
| 95 | + class View(generics.GenericAPIView): |
| 96 | + serializer_class = Serializer |
| 97 | + |
| 98 | + view = create_view( |
| 99 | + View, |
| 100 | + method, |
| 101 | + create_request(path) |
| 102 | + ) |
| 103 | + inspector = OpenAPIAutoSchema() |
| 104 | + inspector.view = view |
| 105 | + |
| 106 | + request_body = inspector._get_request_body(path, method) |
| 107 | + assert request_body['content']['application/json']['required'] == ['text'] |
| 108 | + assert list(request_body['content']['application/json']['properties'].keys()) == ['text'] |
| 109 | + |
| 110 | + def test_response_body_generation(self): |
| 111 | + path = '/' |
| 112 | + method = 'POST' |
| 113 | + |
| 114 | + class Serializer(serializers.Serializer): |
| 115 | + text = serializers.CharField() |
| 116 | + write_only = serializers.CharField(write_only=True) |
| 117 | + |
| 118 | + class View(generics.GenericAPIView): |
| 119 | + serializer_class = Serializer |
| 120 | + |
| 121 | + view = create_view( |
| 122 | + View, |
| 123 | + method, |
| 124 | + create_request(path) |
| 125 | + ) |
| 126 | + inspector = OpenAPIAutoSchema() |
| 127 | + inspector.view = view |
| 128 | + |
| 129 | + responses = inspector._get_responses(path, method) |
| 130 | + assert responses['200']['content']['application/json']['required'] == ['text'] |
| 131 | + assert list(responses['200']['content']['application/json']['properties'].keys()) == ['text'] |
| 132 | + |
87 | 133 |
|
88 | 134 | @pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.') |
89 | 135 | @override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.inspectors.OpenAPIAutoSchema'}) |
|
0 commit comments