From 159c92ffbde0e0c5272a541743c1c992d23a7536 Mon Sep 17 00:00:00 2001 From: Bill Collins Date: Sun, 20 Mar 2022 16:39:48 +0000 Subject: [PATCH] Handle pagination --- rest_framework/schemas/openapi.py | 10 +++++++++- tests/schemas/test_openapi.py | 17 ++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index bb4d1d22a..35b0900f5 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -185,6 +185,9 @@ class AutoSchema(ViewInspector): return component_name + def get_paginated_component_name(self, serializer): + return 'Paginated' + self.get_component_name(serializer) + def get_error_component_name(self, serializer): return self.get_component_name(serializer) + 'Error' @@ -695,6 +698,9 @@ class AutoSchema(ViewInspector): def _get_reference(self, serializer): return {'$ref': '#/components/schemas/{}'.format(self.get_component_name(serializer))} + 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))} @@ -742,7 +748,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 fbe294509..524eaf6f3 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -472,13 +472,7 @@ class TestOperationIntrospection(TestCase): 'content': { 'application/json': { 'schema': { - 'type': 'object', - 'item': { - 'type': 'array', - 'items': { - '$ref': '#/components/schemas/Item' - }, - }, + '$ref': '#/components/schemas/PaginatedItem', }, }, }, @@ -486,6 +480,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': {