Corrected filtering of read/write-only fields.

Closes #6534.
This commit is contained in:
Carlton Gibson 2019-03-26 21:11:16 +01:00
parent aee69bd6c8
commit 61c0c8f17b
2 changed files with 50 additions and 3 deletions

View File

@ -719,7 +719,7 @@ class OpenAPIAutoSchema(ViewInspector):
# No read_only fields for request. # No read_only fields for request.
for name, schema in content['properties'].copy().items(): for name, schema in content['properties'].copy().items():
if 'readOnly' in schema: if 'readOnly' in schema:
del content['properties']['name'] del content['properties'][name]
return { return {
'content': {ct: content for ct in self.content_types} 'content': {ct: content for ct in self.content_types}
@ -744,7 +744,8 @@ class OpenAPIAutoSchema(ViewInspector):
# No write_only fields for response. # No write_only fields for response.
for name, schema in content['properties'].copy().items(): for name, schema in content['properties'].copy().items():
if 'writeOnly' in schema: if 'writeOnly' in schema:
del content['properties']['name'] del content['properties'][name]
content['required'] = [f for f in content['required'] if f != name]
return { return {
'200': { '200': {

View File

@ -2,7 +2,7 @@ import pytest
from django.conf.urls import url from django.conf.urls import url
from django.test import RequestFactory, TestCase, override_settings 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.compat import uritemplate
from rest_framework.request import Request from rest_framework.request import Request
from rest_framework.schemas.generators import OpenAPISchemaGenerator 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.') @pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
@override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.inspectors.OpenAPIAutoSchema'}) @override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.inspectors.OpenAPIAutoSchema'})