Skip to content

Commit

Permalink
Replaced 'TODO' hardcoded version info by a parameter with default '0…
Browse files Browse the repository at this point in the history
….1.0' (encode#6899)
  • Loading branch information
ysavary authored and sigvef committed Dec 3, 2022
1 parent 98c6b85 commit b67d299
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
5 changes: 4 additions & 1 deletion docs/api-guide/schemas.md
Expand Up @@ -60,7 +60,8 @@ urlpatterns = [
# * Provide view name for use with `reverse()`.
path('openapi', get_schema_view(
title="Your Project",
description="API for all things …"
description="API for all things …",
version="1.0.0"
), name='openapi-schema'),
# ...
]
Expand All @@ -72,6 +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`.
* `url`: May be used to pass a canonical base URL for the schema.

schema_view = get_schema_view(
Expand Down Expand Up @@ -137,6 +139,7 @@ Arguments:

* `title` **required**: The name of the API.
* `description`: Longer descriptive text.
* `version`: The version of the API. Defaults to `0.1.0`.
* `url`: The root URL of the API schema. This option is not required unless the schema is included under path prefix.
* `patterns`: A list of URLs to inspect when generating the schema. Defaults to the project's URL conf.
* `urlconf`: A URL conf module name to use when generating the schema. Defaults to `settings.ROOT_URLCONF`.
Expand Down
5 changes: 3 additions & 2 deletions rest_framework/schemas/__init__.py
Expand Up @@ -31,7 +31,8 @@ def get_schema_view(
title=None, url=None, description=None, urlconf=None, renderer_classes=None,
public=False, patterns=None, generator_class=None,
authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES):
permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES,
version=None):
"""
Return a schema view.
"""
Expand All @@ -43,7 +44,7 @@ def get_schema_view(

generator = generator_class(
title=title, url=url, description=description,
urlconf=urlconf, patterns=patterns,
urlconf=urlconf, patterns=patterns, version=version
)

# Avoid import cycle on APIView
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/schemas/coreapi.py
Expand Up @@ -124,7 +124,7 @@ class SchemaGenerator(BaseSchemaGenerator):
# Set by 'SCHEMA_COERCE_METHOD_NAMES'.
coerce_method_names = None

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

Expand Down
3 changes: 2 additions & 1 deletion rest_framework/schemas/generators.py
Expand Up @@ -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):
def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None, version='0.1.0'):
if url and not url.endswith('/'):
url += '/'

Expand All @@ -161,6 +161,7 @@ def __init__(self, title=None, url=None, description=None, patterns=None, urlcon
self.urlconf = urlconf
self.title = title
self.description = description
self.version = version
self.url = url
self.endpoints = None

Expand Down
2 changes: 1 addition & 1 deletion rest_framework/schemas/openapi.py
Expand Up @@ -24,7 +24,7 @@ class SchemaGenerator(BaseSchemaGenerator):
def get_info(self):
info = {
'title': self.title,
'version': 'TODO',
'version': self.version,
}

if self.description is not None:
Expand Down
14 changes: 14 additions & 0 deletions tests/schemas/test_openapi.py
Expand Up @@ -556,3 +556,17 @@ def test_schema_construction(self):

assert 'openapi' in schema
assert 'paths' in schema

def test_schema_information(self):
"""Construction of the top level dictionary."""
patterns = [
url(r'^example/?$', views.ExampleListView.as_view()),
]
generator = SchemaGenerator(patterns=patterns, title='My title', version='1.2.3', description='My description')

request = create_request('/')
schema = generator.get_schema(request=request)

assert schema['info']['title'] == 'My title'
assert schema['info']['version'] == '1.2.3'
assert schema['info']['description'] == 'My description'

0 comments on commit b67d299

Please sign in to comment.