Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial serializer should not have required fields (OpenAPI) #7563

Merged
merged 2 commits into from Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rest_framework/schemas/openapi.py
Expand Up @@ -526,7 +526,7 @@ def map_serializer(self, serializer):
if isinstance(field, serializers.HiddenField):
continue

if field.required:
if field.required and not serializer.partial:
required.append(self.get_field_name(field))

schema = self.map_field(field)
Expand Down
50 changes: 50 additions & 0 deletions tests/schemas/test_openapi.py
Expand Up @@ -403,6 +403,56 @@ class View(generics.GenericAPIView):
assert list(schema['properties']['nested']['properties'].keys()) == ['number']
assert schema['properties']['nested']['required'] == ['number']

def test_response_body_partial_serializer(self):
path = '/'
method = 'GET'

class ItemSerializer(serializers.Serializer):
text = serializers.CharField()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.partial = True

class View(generics.GenericAPIView):
serializer_class = ItemSerializer

view = create_view(
View,
method,
create_request(path),
)
inspector = AutoSchema()
inspector.view = view

responses = inspector.get_responses(path, method)
assert responses == {
'200': {
'description': '',
'content': {
'application/json': {
'schema': {
'type': 'array',
'items': {
'$ref': '#/components/schemas/Item'
},
},
},
},
},
}
components = inspector.get_components(path, method)
assert components == {
'Item': {
'type': 'object',
'properties': {
'text': {
'type': 'string',
},
},
}
}

def test_list_response_body_generation(self):
"""Test that an array schema is returned for list views."""
path = '/'
Expand Down