Skip to content

Commit

Permalink
add setting for listing/paginating/filtering on non-2XX #402 #277
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Jul 23, 2021
1 parent f7f539b commit 0c1fe0e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ def _get_response_for_code(self, serializer, status_code, media_types=None):
if (
self._is_list_view(serializer)
and get_override(serializer, 'many') is not False
and '200' <= status_code < '300'
and ('200' <= status_code < '300' or spectacular_settings.ENABLE_LIST_MECHANICS_ON_NON_2XX)
):
schema = build_array_type(schema)
paginator = self._get_paginator()
Expand Down
6 changes: 6 additions & 0 deletions drf_spectacular/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@
# the order they arrived. Accepts either True, False, or a callable for sort's key arg.
'SORT_OPERATION_PARAMETERS': True,

# @extend_schema allows to specify status codes besides 200. This functionality is usually used
# to describe error responses, which rarely make use of list mechanics. Therefore, we suppress
# listing (pagination and filtering) on non-2XX status codes by default. Toggle this to enable
# list responses with ListSerializers/many=True irrespective of the status code.
'ENABLE_LIST_MECHANICS_ON_NON_2XX': False,

# Option for turning off error and warn messages
'DISABLE_ERRORS_AND_WARNINGS': False,

Expand Down
26 changes: 25 additions & 1 deletion tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ def get(self, request):
}


def test_serializer_method_field_with_functools_partial():
def test_serializer_method_field_with_functools_partial(no_warnings):
class XSerializer(serializers.Serializer):
foo = serializers.SerializerMethodField()
bar = serializers.SerializerMethodField()
Expand All @@ -2052,3 +2052,27 @@ def view_func(request, format=None):
'foo': {'type': 'string', 'format': 'date', 'readOnly': True},
'bar': {'type': 'integer', 'readOnly': True}
}


@mock.patch(
'drf_spectacular.settings.spectacular_settings.ENABLE_LIST_MECHANICS_ON_NON_2XX', True
)
def test_disable_list_mechanics_on_non_2XX(no_warnings):
@extend_schema(
request=SimpleSerializer,
responses={
200: SimpleSerializer(many=True),
400: SimpleSerializer(many=True),
}
)
@api_view(['POST'])
def view_func(request, format=None):
pass # pragma: no cover

schema = generate_schema('/x/', view_function=view_func)
assert get_response_schema(schema['paths']['/x/']['post'], status='200') == {
'type': 'array', 'items': {'$ref': '#/components/schemas/Simple'}
}
assert get_response_schema(schema['paths']['/x/']['post'], status='400') == {
'type': 'array', 'items': {'$ref': '#/components/schemas/Simple'}
}

0 comments on commit 0c1fe0e

Please sign in to comment.