Skip to content

Commit 61c0c8f

Browse files
committed
Corrected filtering of read/write-only fields.
Closes #6534.
1 parent aee69bd commit 61c0c8f

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

rest_framework/schemas/inspectors.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ def _get_request_body(self, path, method):
719719
# No read_only fields for request.
720720
for name, schema in content['properties'].copy().items():
721721
if 'readOnly' in schema:
722-
del content['properties']['name']
722+
del content['properties'][name]
723723

724724
return {
725725
'content': {ct: content for ct in self.content_types}
@@ -744,7 +744,8 @@ def _get_responses(self, path, method):
744744
# No write_only fields for response.
745745
for name, schema in content['properties'].copy().items():
746746
if 'writeOnly' in schema:
747-
del content['properties']['name']
747+
del content['properties'][name]
748+
content['required'] = [f for f in content['required'] if f != name]
748749

749750
return {
750751
'200': {

tests/schemas/test_openapi.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.conf.urls import url
33
from django.test import RequestFactory, TestCase, override_settings
44

5-
from rest_framework import filters, pagination
5+
from rest_framework import filters, generics, pagination, serializers
66
from rest_framework.compat import uritemplate
77
from rest_framework.request import Request
88
from rest_framework.schemas.generators import OpenAPISchemaGenerator
@@ -84,6 +84,52 @@ def test_path_with_id_parameter(self):
8484
},
8585
}]
8686

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+
87133

88134
@pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
89135
@override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.inspectors.OpenAPIAutoSchema'})

0 commit comments

Comments
 (0)