10000 add --generator_class CLI option to generateschema by n2ygk · Pull Request #6735 · encode/django-rest-framework · GitHub
[go: up one dir, main page]

Skip to content

add --generator_class CLI option to generateschema #6735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/community/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ You can determine your currently installed version using `pip show`:
**Date**: [Unreleased][3.10.0-milestone]

* Resolve DeprecationWarning with markdown. [#6317][gh6317]
* Add `generateschema --generator_class` CLI option


## 3.9.x series
Expand Down
7 changes: 6 additions & 1 deletion rest_framework/management/commands/generateschema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.management.base import BaseCommand
from django.utils.module_loading import import_string

from rest_framework import renderers
from rest_framework.schemas import coreapi
Expand All @@ -22,9 +23,13 @@ def add_arguments(self, parser):
parser.add_argument('--format', dest="format", choices=['openapi', 'openapi-json', 'corejson'], default='openapi', type=str)
else:
parser.add_argument('--format', dest="format", choices=['openapi', 'openapi-json'], default='openapi', type=str)
parser.add_argument('--generator_class', dest="generator_class", default=None, type=str)

def handle(self, *args, **options):
generator_class = self.get_generator_class()
if options['generator_class']:
generator_class = import_string(options['generator_class'])
else:
generator_class = self.get_generator_class()
generator = generator_class(
url=options['url'],
title=options['title'],
Expand Down
17 changes: 17 additions & 0 deletions tests/schemas/test_managementcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ def get(self, request):
]


class CustomSchemaGenerator:
SCHEMA = {"key": "value"}

def __init__(self, *args, **kwargs):
pass

def get_schema(self, **kwargs):
return self.SCHEMA


@override_settings(ROOT_URLCONF=__name__)
@pytest.mark.skipif(not uritemplate, reason='uritemplate is not installed')
class GenerateSchemaTests(TestCase):
Expand Down Expand Up @@ -56,6 +66,13 @@ def test_renders_openapi_json_schema(self):
out_json = json.loads(self.out.getvalue())
assert out_json['openapi'] == '3.0.2'

def test_accepts_custom_schema_generator(self):
call_command('generateschema',
'--generator_class={}.{}'.format(__name__, CustomSchemaGenerator.__name__),
stdout=self.out)
out_json = yaml.safe_load(self.out.getvalue())
assert out_json == CustomSchemaGenerator.SCHEMA

@pytest.mark.skipif(yaml is None, reason='PyYAML is required.')
@override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema'})
def test_coreapi_renders_default_schema_with_custom_title_url_and_description(self):
Expand Down
0