OpenAPI: only include non-empty required property. (#6851)

Closes #6834
This commit is contained in:
Chris Vigelius 2019-08-07 21:40:21 +02:00 committed by Carlton Gibson
parent 335054a5d3
commit 0ebfbfdf81
2 changed files with 35 additions and 4 deletions

View File

@ -381,10 +381,14 @@ class AutoSchema(ViewInspector):
self._map_field_validators(field.validators, schema)
properties[field.field_name] = schema
return {
'required': required,
'properties': properties,
result = {
'properties': properties
}
if len(required) > 0:
result['required'] = required
return result
def _map_field_validators(self, validators, schema):
"""
@ -470,7 +474,8 @@ class AutoSchema(ViewInspector):
for name, schema in item_schema['properties'].copy().items():
if 'writeOnly' in schema:
del item_schema['properties'][name]
item_schema['required'] = [f for f in item_schema['required'] if f != name]
if 'required' in item_schema:
item_schema['required'] = [f for f in item_schema['required'] if f != name]
if is_list_view(path, method, self.view):
response_schema = {

View File

@ -142,6 +142,32 @@ class TestOperationIntrospection(TestCase):
assert request_body['content']['application/json']['schema']['required'] == ['text']
assert list(request_body['content']['application/json']['schema']['properties'].keys()) == ['text']
def test_empty_required(self):
path = '/'
method = 'POST'
class Serializer(serializers.Serializer):
read_only = serializers.CharField(read_only=True)
write_only = serializers.CharField(write_only=True, required=False)
class View(generics.GenericAPIView):
serializer_class = Serializer
view = create_view(
View,
method,
create_request(path)
)
inspector = AutoSchema()
inspector.view = view
request_body = inspector._get_request_body(path, method)
# there should be no empty 'required' property, see #6834
assert 'required' not in request_body['content']['application/json']['schema']
for response in inspector._get_responses(path, method).values():
assert 'required' not in response['content']['application/json']['schema']
def test_response_body_generation(self):
path = '/'
method = 'POST'