axnsan12 / drf-yasg

Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
https://drf-yasg.readthedocs.io/en/stable/
Other
3.4k stars 438 forks source link

Add javascript hooks in UI init scripts #294

Open oneandonlyonebutyou opened 5 years ago

oneandonlyonebutyou commented 5 years ago

I wonder how I could add configs to the swagger settings. Thanks. something like this :

https://github.com/swagger-api/swagger-ui/issues/3229

  // Build a system
  const ui = SwaggerUIBundle({
    ...
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    presets_config: {
       SwaggerUIStandalonePreset: {
           TopbarPlugin: false
       }
    }
    ...
  })

  window.ui = ui
}
axnsan12 commented 5 years ago

You can always deploy your own swagger-ui instance instead of using the bundled one.

Unfortunately no, there is no simple way to plug that into the bundled one.

oneandonlyonebutyou commented 5 years ago

Thanks for fast response. (means a lot) I would love to contribute soon ..

rsichnyi commented 5 years ago

@joseph-vedadi this is a bit hacky, but what you could do is:

  1. add your own template (drf-yasg.html):
    
    {% extends "drf-yasg/swagger-ui.html" %}

{% block extra_scripts %}

{% endblock %}


2. change your `urls.py`:

from drf_yasg.views import SwaggerUIRenderer, get_schema_view

SwaggerUIRenderer.template = 'drf-yasg.html' # monkey-patching is bad :(

schema_view = get_schema_view( openapi.Info( title="My API", default_version='1.0.0', description="My API" ) )

axnsan12 commented 5 years ago

Monkey patching shouldn't be necessary, the template can be overridden using the standard Django mechanism - place it at the same path drf-yasg/swagger-ui.html in the templates directory of an app.

The app should come before drf-yasg in INSTALLED_APPS.

rsichnyi commented 5 years ago

@axnsan12 iirc in that case {% extends "drf-yasg/swagger-ui.html" %} will fail because it'll become cyclic dependency