Add --generator_class CLI option to generateschema (#6735)

* add --generator_class CLI option to generateschema
* Add test for generateschema —generator_class flag.
This commit is contained in:
Alan Crosswell 2019-06-09 08:43:54 -04:00 committed by Carlton Gibson
parent 2d65f82dd7
commit 819c46ea80
3 changed files with 24 additions and 1 deletions

View File

@ -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

View File

@ -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
@ -22,9 +23,13 @@ class Command(BaseCommand):
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'],

View File

@ -22,6 +22,16 @@ urlpatterns = [
]
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):
@ -56,6 +66,13 @@ class GenerateSchemaTests(TestCase):
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):