Repository: encode/django-rest-framework

Issue:
pyyaml required for openapi-json schemas
## Checklist

- [x] I have verified that that issue exists against the `master` branch of Django REST framework.
- [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- [x] This is not a usage question. (Those should be directed to the [discussion group](https://groups.google.com/forum/#!forum/django-rest-framework) instead.)
- [x] This cannot be dealt with as a third party library. (We prefer new functionality to be [in the form of third party libraries](https://www.django-rest-framework.org/topics/third-party-resources/#about-third-party-packages) where possible.)
- [x] I have reduced the issue to the simplest possible case.
- [ ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

## Steps to reproduce
- (assuming DRF is set up and has API endpoints for which to generate a schema)
- Install coreapi, but not pyyaml
- run `python manage.py generateschema --format=openapi-json

## Expected behavior
- The openapi-json schema is printed to stdout

## Actual behavior
```
Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/management/commands/generateschema.py", line 30, in handle
    renderer = self.get_renderer(options['format'])
  File "/usr/local/lib/python3.7/site-packages/rest_framework/management/commands/generateschema.py", line 37, in get_renderer
    'openapi': OpenAPIRenderer(),
  File "/usr/local/lib/python3.7/site-packages/rest_framework/renderers.py", line 1033, in __init__
    assert yaml, 'Using OpenAPIRenderer, but `pyyaml` is not installed.'
AssertionError: Using OpenAPIRenderer, but `pyyaml` is not installed.
```

Additional Details
------------------
Installing pyyaml fixes the problem, but it shouldn't be required if only json formatted schemas are being generated.

Additionally, I found the way that it failed to be misleading - I logged #6359 because the stack trace and error message really looked like the format parameter was being ignored, since `--format openapi-json` uses `JsonOpenAPIRenderer`, not `OpenAPIRenderer`.


Discussion:

Resolving patch:
diff --git a/rest_framework/management/commands/generateschema.py b/rest_framework/management/commands/generateschema.py
--- a/rest_framework/management/commands/generateschema.py
+++ b/rest_framework/management/commands/generateschema.py
@@ -32,8 +32,10 @@ def handle(self, *args, **options):
         self.stdout.write(output.decode('utf-8'))
 
     def get_renderer(self, format):
-        return {
-            'corejson': CoreJSONRenderer(),
-            'openapi': OpenAPIRenderer(),
-            'openapi-json': JSONOpenAPIRenderer()
+        renderer_cls = {
+            'corejson': CoreJSONRenderer,
+            'openapi': OpenAPIRenderer,
+            'openapi-json': JSONOpenAPIRenderer,
         }[format]
+
+        return renderer_cls()


Test patch: