Skip to content

Commit f81ca78

Browse files
author
Thorsten
authored
Add file option to generateschema (#7130)
1 parent 4137ef4 commit f81ca78

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

docs/api-guide/schemas.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ into the commonly used YAML-based OpenAPI format.
3030
If your schema is static, you can use the `generateschema` management command:
3131

3232
```bash
33-
./manage.py generateschema > openapi-schema.yml
33+
./manage.py generateschema --file openapi-schema.yml
3434
```
3535

3636
Once you've generated a schema in this way you can annotate it with any

rest_framework/management/commands/generateschema.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def add_arguments(self, parser):
2525
parser.add_argument('--format', dest="format", choices=['openapi', 'openapi-json'], default='openapi', type=str)
2626
parser.add_argument('--urlconf', dest="urlconf", default=None, type=str)
2727
parser.add_argument('--generator_class', dest="generator_class", default=None, type=str)
28+
parser.add_argument('--file', dest="file", default=None, type=str)
2829

2930
def handle(self, *args, **options):
3031
if options['generator_class']:
@@ -40,7 +41,12 @@ def handle(self, *args, **options):
4041
schema = generator.get_schema(request=None, public=True)
4142
renderer = self.get_renderer(options['format'])
4243
output = renderer.render(schema, renderer_context={})
43-
self.stdout.write(output.decode())
44+
45+
if options['file']:
46+
with open(options['file'], 'wb') as f:
47+
f.write(output)
48+
else:
49+
self.stdout.write(output.decode())
4450

4551
def get_renderer(self, format):
4652
if self.get_mode() == COREAPI_MODE:

tests/schemas/test_managementcommand.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import io
2+
import os
3+
import tempfile
24

35
import pytest
46
from django.conf.urls import url
@@ -73,6 +75,21 @@ def test_accepts_custom_schema_generator(self):
7375
out_json = yaml.safe_load(self.out.getvalue())
7476
assert out_json == CustomSchemaGenerator.SCHEMA
7577

78+
def test_writes_schema_to_file_on_parameter(self):
79+
fd, path = tempfile.mkstemp()
80+
try:
81+
call_command('generateschema', '--file={}'.format(path), stdout=self.out)
82+
# nothing on stdout
83+
assert not self.out.getvalue()
84+
85+
call_command('generateschema', stdout=self.out)
86+
expected_out = self.out.getvalue()
87+
# file output identical to stdout output
88+
with os.fdopen(fd) as fh:
89+
assert expected_out and fh.read() == expected_out
90+
finally:
91+
os.remove(path)
92+
7693
@pytest.mark.skipif(yaml is None, reason='PyYAML is required.')
7794
@override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema'})
7895
def test_coreapi_renders_default_schema_with_custom_title_url_and_description(self):

0 commit comments

Comments
 (0)