/drf-swagger-customization

This is a django app which you can modify and improve your autogenerated swagger documentation from your drf API.

Primary LanguagePythonMIT LicenseMIT

drf-swagger-customization

https://img.shields.io/badge/docs-latest-brightgreen.svg?style=flat

This is a django app which you can modify and improve your autogenerated swagger documentation from your drf API.

Documentation

The full documentation is at https://drf-swagger-customization.readthedocs.io.

Quickstart

Install drf-swagger-customization:

pip install drf-swagger-customization

Add these global variables to your settings.py:

EXTENSION_PATH = os.path.join(PROJECT_DIR, 'docs/doc_extension.json') # Path to your extension file
EXTERNAL_DOC_FOLDER = os.path.join(PROJECT_DIR, 'docs/external/') # Path to your external documentation folder

Add drf-swagger-customization's URL patterns:

from drf_swagger_customization.views import get_swagger_view

schema_view = get_swagger_view(title='Pastebin API')

urlpatterns = [
    ...
    url(r'^docs/$', schema_view),
    ...
]

Usage

With this package we can increase the auto-generated documentation from django-swagger. That way, we can add documentation from external APIs or add more information to our drf API methods such as fields, remove endpoints, update attributes, and so on.

In order to add information to our EXTENSION_PATH json file, we have available these operations:

Create

{
    "action": "create",
    "operation": {
        "paths|/v1/travels/|get|parameters": [
            {
              "name": "Field1",
              "in": "query",
              "required": true,
              "type": "string"
            },
            {
              "name": "Field2",
              "in": "path",
              "required": true,
              "type": "integer"
            }
        ]
    }
}

Update

{
    "action": "update",
    "operation": {
        "paths|/v1/travels/|get|parameters|field1": {
            "name": "Field1",
            "in": "query",
            "required": true,
            "type": "string"
        }
    }
}

Remove

{
    "action": "delete",
    "operation": "paths|/v1/travels/|get|parameters|field1"
}

Rename

{
    "action": "rename",
    "operation": {
        "paths|/v1/travels/": "/travels/",
        "paths|/v1/travels/{id}/": "/travels/{id}/"
    }
}

Completed Sample

[
  {
    "action": "create",
    "operation": {
      "paths|/v1/travels/|get|parameters": [
        {
          "name": "Field1",
          "in": "query",
          "required": true,
          "type": "string"
        },
        {
          "name": "Field2",
          "in": "path",
          "required": true,
          "type": "integer"
        }
      ]
    }
  },
  {
    "action": "update",
    "operation": {
      "paths|/v1/travels/|get|parameters|field1": {
        "name": "Field1",
        "in": "query",
        "required": true,
        "type": "string"
      }
    }
  },
  {
    "action": "delete",
    "operation": "paths|/v1/travels/|get|parameters|field1"
  },
  {
    "action": "rename",
    "operation": {
        "paths|/v1/travels/": "/travels/",
        "paths|/v1/travels/{id}/": "/travels/{id}/"
    }
  }
]

Future Work

  • Add an example
  • Command for doc_file customization, in order to remove redundant executions.
  • Improve the settings options.