From 0d3d548aa52c92c57bb919c2965b723aee6b903f Mon Sep 17 00:00:00 2001 From: Yann Savary Date: Wed, 6 Nov 2019 21:54:12 +0100 Subject: [PATCH] OpenAPI: Fixed generation when title or version not provided. (#6912) --- docs/api-guide/schemas.md | 2 +- rest_framework/schemas/generators.py | 2 +- rest_framework/schemas/openapi.py | 5 +++-- tests/schemas/test_openapi.py | 13 +++++++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/api-guide/schemas.md b/docs/api-guide/schemas.md index ec5366d8e..e33a2a611 100644 --- a/docs/api-guide/schemas.md +++ b/docs/api-guide/schemas.md @@ -73,7 +73,7 @@ The `get_schema_view()` helper takes the following keyword arguments: * `title`: May be used to provide a descriptive title for the schema definition. * `description`: Longer descriptive text. -* `version`: The version of the API. Defaults to `0.1.0`. +* `version`: The version of the API. * `url`: May be used to pass a canonical base URL for the schema. schema_view = get_schema_view( diff --git a/rest_framework/schemas/generators.py b/rest_framework/schemas/generators.py index 77e92eeb8..4b6d82a14 100644 --- a/rest_framework/schemas/generators.py +++ b/rest_framework/schemas/generators.py @@ -151,7 +151,7 @@ class BaseSchemaGenerator(object): # Set by 'SCHEMA_COERCE_PATH_PK'. coerce_path_pk = None - def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None, version=''): + def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None, version=None): if url and not url.endswith('/'): url += '/' diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index e33759e81..134df5043 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -21,9 +21,10 @@ from .utils import get_pk_description, is_list_view class SchemaGenerator(BaseSchemaGenerator): def get_info(self): + # Title and version are required by openapi specification 3.x info = { - 'title': self.title, - 'version': self.version, + 'title': self.title or '', + 'version': self.version or '' } if self.description is not None: diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 5a56bae87..622f78cdd 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -704,3 +704,16 @@ class TestGenerator(TestCase): assert schema['info']['title'] == 'My title' assert schema['info']['version'] == '1.2.3' assert schema['info']['description'] == 'My description' + + def test_schema_information_empty(self): + """Construction of the top level dictionary.""" + patterns = [ + url(r'^example/?$', views.ExampleListView.as_view()), + ] + generator = SchemaGenerator(patterns=patterns) + + request = create_request('/') + schema = generator.get_schema(request=request) + + assert schema['info']['title'] == '' + assert schema['info']['version'] == ''