diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index af2ceda43..c88244c54 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -192,6 +192,9 @@ class AutoSchema(ViewInspector): return component_name + def get_paginated_component_name(self, serializer): + return 'Paginated' + self.get_component_name(serializer, 'GET') + def get_error_component_name(self, serializer): return self.get_component_name(serializer, 'GET') + 'Error' @@ -716,6 +719,9 @@ class AutoSchema(ViewInspector): def _get_reference(self, serializer, method): return {'$ref': '#/components/schemas/{}'.format(self.get_component_name(serializer, method))} + def _get_paginated_reference(self, serializer): + return {'$ref': '#/components/schemas/{}'.format(self.get_paginated_component_name(serializer))} + def _get_error_reference(self, serializer): return {'$ref': '#/components/schemas/{}'.format(self.get_error_component_name(serializer))} @@ -763,7 +769,9 @@ class AutoSchema(ViewInspector): } paginator = self.get_paginator() if paginator: - response_schema = paginator.get_paginated_response_schema(response_schema) + pagination_schema = paginator.get_paginated_response_schema(response_schema) + self.components[self.get_paginated_component_name(serializer)] = pagination_schema + response_schema = self._get_paginated_reference(serializer) else: response_schema = item_schema status_code = '201' if method == 'POST' else '200' diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index d39f2dbf1..0eca387a8 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -474,13 +474,7 @@ class TestOperationIntrospection(TestCase): 'content': { 'application/json': { 'schema': { - 'type': 'object', - 'item': { - 'type': 'array', - 'items': { - '$ref': '#/components/schemas/Item' - }, - }, + '$ref': '#/components/schemas/PaginatedItem', }, }, }, @@ -488,6 +482,15 @@ class TestOperationIntrospection(TestCase): } components = inspector.get_components(path, method) assert components == { + 'PaginatedItem': { + 'type': 'object', + 'item': { + 'type': 'array', + 'items': { + '$ref': '#/components/schemas/Item' + }, + }, + }, 'Item': { 'type': 'object', 'properties': {