mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-30 18:09:59 +03:00
parent
0379265f13
commit
be3e1287f2
|
@ -719,7 +719,7 @@ class OpenAPIAutoSchema(ViewInspector):
|
|||
# No read_only fields for request.
|
||||
for name, schema in content['properties'].copy().items():
|
||||
if 'readOnly' in schema:
|
||||
del content['properties']['name']
|
||||
del content['properties'][name]
|
||||
|
||||
return {
|
||||
'content': {ct: content for ct in self.content_types}
|
||||
|
@ -744,7 +744,8 @@ class OpenAPIAutoSchema(ViewInspector):
|
|||
# No write_only fields for response.
|
||||
for name, schema in content['properties'].copy().items():
|
||||
if 'writeOnly' in schema:
|
||||
del content['properties']['name']
|
||||
del content['properties'][name]
|
||||
content['required'] = [f for f in content['required'] if f != name]
|
||||
|
||||
return {
|
||||
'200': {
|
||||
|
|
|
@ -2,7 +2,7 @@ import pytest
|
|||
from django.conf.urls import url
|
||||
from django.test import RequestFactory, TestCase, override_settings
|
||||
|
||||
from rest_framework import filters, pagination
|
||||
from rest_framework import filters, generics, pagination, serializers
|
||||
from rest_framework.compat import uritemplate
|
||||
from rest_framework.request import Request
|
||||
from rest_framework.schemas.generators import OpenAPISchemaGenerator
|
||||
|
@ -84,6 +84,52 @@ class TestOperationIntrospection(TestCase):
|
|||
},
|
||||
}]
|
||||
|
||||
def test_request_body(self):
|
||||
path = '/'
|
||||
method = 'POST'
|
||||
|
||||
class Serializer(serializers.Serializer):
|
||||
text = serializers.CharField()
|
||||
read_only = serializers.CharField(read_only=True)
|
||||
|
||||
class View(generics.GenericAPIView):
|
||||
serializer_class = Serializer
|
||||
|
||||
view = create_view(
|
||||
View,
|
||||
method,
|
||||
create_request(path)
|
||||
)
|
||||
inspector = OpenAPIAutoSchema()
|
||||
inspector.view = view
|
||||
|
||||
request_body = inspector._get_request_body(path, method)
|
||||
assert request_body['content']['application/json']['required'] == ['text']
|
||||
assert list(request_body['content']['application/json']['properties'].keys()) == ['text']
|
||||
|
||||
def test_response_body_generation(self):
|
||||
path = '/'
|
||||
method = 'POST'
|
||||
|
||||
class Serializer(serializers.Serializer):
|
||||
text = serializers.CharField()
|
||||
write_only = serializers.CharField(write_only=True)
|
||||
|
||||
class View(generics.GenericAPIView):
|
||||
serializer_class = Serializer
|
||||
|
||||
view = create_view(
|
||||
View,
|
||||
method,
|
||||
create_request(path)
|
||||
)
|
||||
inspector = OpenAPIAutoSchema()
|
||||
inspector.view = view
|
||||
|
||||
responses = inspector._get_responses(path, method)
|
||||
assert responses['200']['content']['application/json']['required'] == ['text']
|
||||
assert list(responses['200']['content']['application/json']['properties'].keys()) == ['text']
|
||||
|
||||
|
||||
@pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
|
||||
@override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.inspectors.OpenAPIAutoSchema'})
|
||||
|
|
Loading…
Reference in New Issue
Block a user