8000 Add --generator_class CLI option to generateschema (#6735) · coderanger/django-rest-framework@819c46e · GitHub
[go: up one dir, main page]

Skip to content

Commit 819c46e

Browse files
n2ygkcarltongibson
authored andcommitted
Add --generator_class CLI option to generateschema (encode#6735)
* add --generator_class CLI option to generateschema * Add test for generateschema —generator_class flag.
1 parent 2d65f82 commit 819c46e

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

docs/community/release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ You can determine your currently installed version using `pip show`:
4545
**Date**: [Unreleased][3.10.0-milestone]
4646

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

4950

5051
## 3.9.x series

rest_framework/management/commands/generateschema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.core.management.base import BaseCommand
2+
from django.utils.module_loading import import_string
23

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

2628
def handle(self, *args, **options):
27-
generator_class = self.get_generator_class()
29+
if options['generator_class']:
30+
generator_class = import_string(options['generator_class'])
31+
else:
32+
generator_class = self.get_generator_class()
2833
generator = generator_class(
2934
url=options['url'],
3035
title=options['title'],

tests/schemas/test_managementcommand.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def get(self, request):
2222
]
2323

2424

25+
class CustomSchemaGenerator:
26+
SCHEMA = {"key": "value"}
27+
28+
def __init__(self, *args, **kwargs):
29+
pass
30+
31+
def get_schema(self, **kwargs):
32+
return self.SCHEMA
33+
34+
2535
@override_settings(ROOT_URLCONF=__name__)
2636
@pytest.mark.skipif(not uritemplate, reason='uritemplate is not installed')
2737
class GenerateSchemaTests(TestCase):
@@ -56,6 +66,13 @@ def test_renders_openapi_json_schema(self):
5666
out_json = json.loads(self.out.getvalue())
5767
assert out_json['openapi'] == '3.0.2'
5868

69+
def test_accepts_custom_schema_generator(self):
70+
call_command('generateschema',
71+
'--generator_class={}.{}'.format(__name__, CustomSchemaGenerator.__name__),
72+
stdout=self.out)
73+
out_json = yaml.safe_load(self.out.getvalue())
74+
assert out_json == CustomSchemaGenerator.SCHEMA
75+
5976
@pytest.mark.skipif(yaml is None, reason='PyYAML is required.')
6077
@override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema'})
6178
def test_coreapi_renders_default_schema_with_custom_title_url_and_description(self):

0 commit comments

Comments
 (0)
0