Add 'generate_swagger' management command (#31)

Closes #29.
This commit is contained in:
Beau Gunderson
2017-12-27 11:00:24 -08:00
committed by Cristi Vîjdea
parent 9f14114520
commit 1f190744cd
18 changed files with 408 additions and 107 deletions
+7
View File
@@ -3,6 +3,13 @@ Changelog
#########
*********
**1.1.1**
*********
- **ADDED:** :ref:`generate_swagger management command <management-command>`
(:issue:`29`, :pr:`31`, thanks to :ghuser:`beaugunderson`)
*********
**1.1.0**
*********
+10 -10
View File
@@ -138,7 +138,7 @@ The ``@swagger_auto_schema`` decorator
You can use the :func:`@swagger_auto_schema <.swagger_auto_schema>` decorator on view functions to override
some properties of the generated :class:`.Operation`. For example, in a ``ViewSet``,
.. code:: python
.. code-block:: python
@swagger_auto_schema(operation_description="partial_update description override", responses={404: 'slug not found'})
def partial_update(self, request, *args, **kwargs):
@@ -153,7 +153,7 @@ Where you can use the :func:`@swagger_auto_schema <.swagger_auto_schema>` decora
* for function based ``@api_view``\ s, because the same view can handle multiple methods, and thus represent multiple
operations, you have to add the decorator multiple times if you want to override different operations:
.. code:: python
.. code-block:: python
test_param = openapi.Parameter('test', openapi.IN_QUERY, description="test manual param", type=openapi.TYPE_BOOLEAN)
user_response = openapi.Response('response description', UserSerializer)
@@ -169,7 +169,7 @@ Where you can use the :func:`@swagger_auto_schema <.swagger_auto_schema>` decora
* for class based ``APIView``, ``GenericAPIView`` and non-``ViewSet`` derivatives, you have to decorate the respective
method of each operation:
.. code:: python
.. code-block:: python
class UserList(APIView):
@swagger_auto_schema(responses={200: UserSerializer(many=True)})
@@ -186,7 +186,7 @@ Where you can use the :func:`@swagger_auto_schema <.swagger_auto_schema>` decora
respond to multiple HTTP methods and thus have multiple operations that must be decorated separately:
.. code:: python
.. code-block:: python
class ArticleViewSet(viewsets.ModelViewSet):
# method or 'methods' can be skipped because the list_route only handles a single method (GET)
@@ -214,7 +214,7 @@ Where you can use the :func:`@swagger_auto_schema <.swagger_auto_schema>` decora
If you want to customize the generation of a method you are not implementing yourself, you can use
``swagger_auto_schema`` in combination with Django's ``method_decorator``:
.. code:: python
.. code-block:: python
@method_decorator(name='list', decorator=swagger_auto_schema(
operation_description="description from swagger_auto_schema via method_decorator"
@@ -229,7 +229,7 @@ Where you can use the :func:`@swagger_auto_schema <.swagger_auto_schema>` decora
You can go even further and directly decorate the result of ``as_view``, in the same manner you would
override an ``@api_view`` as described above:
.. code:: python
.. code-block:: python
decorated_login_view = \
swagger_auto_schema(
@@ -256,7 +256,7 @@ Serializer ``Meta`` nested class
You can define some per-serializer options by adding a ``Meta`` class to your serializer, e.g.:
.. code:: python
.. code-block:: python
class WhateverSerializer(Serializer):
...
@@ -288,7 +288,7 @@ class-level attribute named ``swagger_schema`` on the view class, or
For example, to generate all operation IDs as camel case, you could do:
.. code:: python
.. code-block:: python
from inflection import camelize
@@ -331,7 +331,7 @@ For customizing behavior related to specific field, serializer, filter or pagina
A :class:`~.inspectors.FilterInspector` that adds a description to all ``DjangoFilterBackend`` parameters could be
implemented like so:
.. code:: python
.. code-block:: python
class DjangoFilterDescriptionInspector(CoreAPICompatInspector):
def get_filter_parameters(self, filter_backend):
@@ -357,7 +357,7 @@ implemented like so:
A second example, of a :class:`~.inspectors.FieldInspector` that removes the ``title`` attribute from all generated
:class:`.Schema` objects:
.. code:: python
.. code-block:: python
class NoSchemaTitleInspector(FieldInspector):
def process_result(self, result, method_name, obj, **kwargs):
+26 -1
View File
@@ -2,6 +2,7 @@
Serving the schema
##################
************************************************
``get_schema_view`` and the ``SchemaView`` class
************************************************
@@ -14,7 +15,7 @@ in the README for a usage example.
You can also subclass :class:`.SchemaView` by extending the return value of :func:`.get_schema_view`, e.g.:
.. code:: python
.. code-block:: python
SchemaView = get_schema_view(info, ...)
@@ -33,3 +34,27 @@ codec and the view.
You can use your custom renderer classes as kwargs to :meth:`.SchemaView.as_cached_view` or by subclassing
:class:`.SchemaView`.
.. _management-command:
******************
Management command
******************
.. versionadded:: 1.1.1
If you only need a swagger spec file in YAML or JSON format, you can use the ``generate_swagger`` management command
to get it without having to start the web server:
.. code-block:: console
$ python manage.py generate_swagger swagger.json
See the command help for more advanced options:
.. code-block:: console
$ python manage.py generate_swagger --help
usage: manage.py generate_swagger [-h] [--version] [-v {0,1,2,3}]
... more options ...
+21 -2
View File
@@ -15,7 +15,7 @@ Example:
**settings.py**
.. code:: python
.. code-block:: python
SWAGGER_SETTINGS = {
'SECURITY_DEFINITIONS': {
@@ -91,6 +91,25 @@ Paginator inspectors given to :func:`@swagger_auto_schema <.swagger_auto_schema>
:class:`'drf_yasg.inspectors.CoreAPICompatInspector' <.inspectors.CoreAPICompatInspector>`, |br| \
``]``
Swagger document attributes
===========================
DEFAULT_INFO
------------
An import string to an :class:`.openapi.Info` object. This will be used when running the ``generate_swagger``
management command, or if no ``info`` argument is passed to ``get_schema_view``.
**Default**: :python:`None`
DEFAULT_API_URL
---------------
A string representing the default API URL. This will be used to populate the ``host``, ``schemes`` and ``basePath``
attributes of the Swagger document if no API URL is otherwise provided.
**Default**: :python:`''`
Authorization
=============
@@ -124,7 +143,7 @@ See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#sec
**Default**:
.. code:: python
.. code-block:: python
'basic': {
'type': 'basic'