Skip to content

Commit e57c150

Browse files
ysavarycarltongibson
authored andcommitted
Replaced 'TODO' hardcoded version info by a parameter with default '0.1.0' (#6899)
1 parent b3f032f commit e57c150

File tree

6 files changed

+25
-6
lines changed

6 files changed

+25
-6
lines changed

docs/api-guide/schemas.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ urlpatterns = [
6060
# * Provide view name for use with `reverse()`.
6161
path('openapi', get_schema_view(
6262
title="Your Project",
63-
description="API for all things …"
63+
description="API for all things …",
64+
version="1.0.0"
6465
), name='openapi-schema'),
6566
# ...
6667
]
@@ -72,6 +73,7 @@ The `get_schema_view()` helper takes the following keyword arguments:
7273

7374
* `title`: May be used to provide a descriptive title for the schema definition.
7475
* `description`: Longer descriptive text.
76+
* `version`: The version of the API. Defaults to `0.1.0`.
7577
* `url`: May be used to pass a canonical base URL for the schema.
7678

7779
schema_view = get_schema_view(
@@ -137,6 +139,7 @@ Arguments:
137139

138140
* `title` **required**: The name of the API.
139141
* `description`: Longer descriptive text.
142+
* `version`: The version of the API. Defaults to `0.1.0`.
140143
* `url`: The root URL of the API schema. This option is not required unless the schema is included under path prefix.
141144
* `patterns`: A list of URLs to inspect when generating the schema. Defaults to the project's URL conf.
142145
* `urlconf`: A URL conf module name to use when generating the schema. Defaults to `settings.ROOT_URLCONF`.

rest_framework/schemas/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def get_schema_view(
3131
title=None, url=None, description=None, urlconf=None, renderer_classes=None,
3232
public=False, patterns=None, generator_class=None,
3333
authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
34-
permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES):
34+
permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES,
35+
version=None):
3536
"""
3637
Return a schema view.
3738
"""
@@ -43,7 +44,7 @@ def get_schema_view(
4344

4445
generator = generator_class(
4546
title=title, url=url, description=description,
46-
urlconf=urlconf, patterns=patterns,
47+
urlconf=urlconf, patterns=patterns, version=version
4748
)
4849

4950
# Avoid import cycle on APIView

rest_framework/schemas/coreapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class SchemaGenerator(BaseSchemaGenerator):
124124
# Set by 'SCHEMA_COERCE_METHOD_NAMES'.
125125
coerce_method_names = None
126126

127-
def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None):
127+
def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None, version=None):
128128
assert coreapi, '`coreapi` must be installed for schema support.'
129129
assert coreschema, '`coreschema` must be installed for schema support.'
130130

rest_framework/schemas/generators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class BaseSchemaGenerator(object):
151151
# Set by 'SCHEMA_COERCE_PATH_PK'.
152152
coerce_path_pk = None
153153

154-
def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None):
154+
def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None, version='0.1.0'):
155155
if url and not url.endswith('/'):
156156
url += '/'
157157

@@ -161,6 +161,7 @@ def __init__(self, title=None, url=None, description=None, patterns=None, urlcon
161161
self.urlconf = urlconf
162162
self.title = title
163163
self.description = description
164+
self.version = version
164165
self.url = url
165166
self.endpoints = None
166167

rest_framework/schemas/openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SchemaGenerator(BaseSchemaGenerator):
2424
def get_info(self):
2525
info = {
2626
'title': self.title,
27-
'version': 'TODO',
27+
'version': self.version,
2828
}
2929

3030
if self.description is not None:

tests/schemas/test_openapi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,17 @@ def test_schema_construction(self):
556556

557557
assert 'openapi' in schema
558558
assert 'paths' in schema
559+
560+
def test_schema_information(self):
561+
"""Construction of the top level dictionary."""
562+
patterns = [
563+
url(r'^example/?$', views.ExampleListView.as_view()),
564+
]
565+
generator = SchemaGenerator(patterns=patterns, title='My title', version='1.2.3', description='My description')
566+
567+
request = create_request('/')
568+
schema = generator.get_schema(request=request)
569+
570+
assert schema['info']['title'] == 'My title'
571+
assert schema['info']['version'] == '1.2.3'
572+
assert schema['info']['description'] == 'My description'

0 commit comments

Comments
 (0)