vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
6.97k stars 420 forks source link

A way to pass arguments to SwaggerUI (like `persistAuthorization`) #782

Open pawelngei opened 1 year ago

pawelngei commented 1 year ago

Is your feature request related to a problem? Please describe. Currently Django-Ninja's Swagger doesn't remember tokens between refresh, which can be pretty annoying when developing permission-heavy endpoints requiring refreshing the docs. As far as I'm aware, the openapi_extra doesn't pass any parameters (like https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ ) to SwaggerUI itself.

Describe the solution you'd like We could introduce a new parameter, like swaggerui_params (and maybe a matching redoc_params), which will be a dictionary of parameters passed to each UI (for example via swagger.html or swagger-ui-init.js)

Alternatively, we could set persistAuthorization as a separate boolean option to NinjaAPI, as just persist_doc_authorization=true. This will be a simpler case, but might be limiting to the users in the long run.

Should we implement either of these, it would be good to put an example in the docs, especially https://django-ninja.rest-framework.com/guides/api-docs/ .

vitalik commented 1 year ago

Hi @pawelngei

yeah, some extensions would be nice... but I would not make it a separate parameter like NinjaAPI(swagger_params={}) - the idea for future is add ability to add way more OpenAPI doc viewers as plugins (swagger, redocs, rapidocs, etc)

I think most flexible approach would be to just add a context that will be passed to a html template that renders that doc engine.. like:


api = NinjaAPI(docs_context={'swagger': {'persistAuthorization': True}}}
pawelngei commented 1 year ago

If I was to write the code (can't promise I'll have time right now), would it be enough to pass and test docs_context for swagger only, or should I test redoc as well? Should I update the docs, or leave it to you / other maintainers?

pawelngei commented 1 year ago

Created a PR implementing both Swagger, Redoc and tests, hopefully it's good enough to get merged ;)