From e438ccde89687298792584b5d43913a756201be9 Mon Sep 17 00:00:00 2001 From: Calancea Daniel Date: Wed, 7 Sep 2022 20:47:03 +0300 Subject: [PATCH 1/2] Added option for runtime configuration --- lib/open_api_spex/plug/swagger_ui.ex | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/open_api_spex/plug/swagger_ui.ex b/lib/open_api_spex/plug/swagger_ui.ex index 8e162956..1c2af612 100644 --- a/lib/open_api_spex/plug/swagger_ui.ex +++ b/lib/open_api_spex/plug/swagger_ui.ex @@ -12,6 +12,12 @@ defmodule OpenApiSpex.Plug.SwaggerUI do See the [swagger-ui configuration docs](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/) for details. Should dynamic configuration be required, the `config_url` option can be set to an API endpoint that will provide additional config. + ## Runtime Configuration + + SwaggerUI also accepts functions as parameters, allowing runtime configurations. + Because of limitations of `router.ex` file, the functions can be defined only as: `{module, function_name}`. + Example: `{MyAppWeb.RuntimeConfig, :on_complete}` + ## Example scope "/" do @@ -22,6 +28,7 @@ defmodule OpenApiSpex.Plug.SwaggerUI do path: "/api/openapi", default_model_expand_depth: 3, display_operation_id: true + on_complete: {MyAppWeb.RuntimeConfig, :on_complete} end # Other scopes may use custom stacks. @@ -153,6 +160,7 @@ defmodule OpenApiSpex.Plug.SwaggerUI do def call(conn, config) do csrf_token = Plug.CSRFProtection.get_csrf_token() config = supplement_config(config, conn) + config = evaluate_runtime_config(config) html = render(config, csrf_token) conn @@ -203,4 +211,16 @@ defmodule OpenApiSpex.Plug.SwaggerUI do defp supplement_config(config, _conn) do config end + + defp evaluate_runtime_config(config) do + Enum.into(config, %{}, fn opt -> evaluate_opt(opt) end) + end + + defp evaluate_opt({key, {module, function_name}}) when is_atom(module) and is_atom(function_name) do + {key, apply(module, function_name, [])} + end + + defp evaluate_opt(opt) do + opt + end end From 966b5597a58110fe705bbe7f4db2a51b2785e0be Mon Sep 17 00:00:00 2001 From: Calancea Daniel Date: Wed, 7 Sep 2022 20:56:28 +0300 Subject: [PATCH 2/2] Updated plug documentation --- lib/open_api_spex/plug/swagger_ui.ex | 29 +++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/open_api_spex/plug/swagger_ui.ex b/lib/open_api_spex/plug/swagger_ui.ex index 1c2af612..f2346d8c 100644 --- a/lib/open_api_spex/plug/swagger_ui.ex +++ b/lib/open_api_spex/plug/swagger_ui.ex @@ -12,12 +12,6 @@ defmodule OpenApiSpex.Plug.SwaggerUI do See the [swagger-ui configuration docs](https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/) for details. Should dynamic configuration be required, the `config_url` option can be set to an API endpoint that will provide additional config. - ## Runtime Configuration - - SwaggerUI also accepts functions as parameters, allowing runtime configurations. - Because of limitations of `router.ex` file, the functions can be defined only as: `{module, function_name}`. - Example: `{MyAppWeb.RuntimeConfig, :on_complete}` - ## Example scope "/" do @@ -28,7 +22,6 @@ defmodule OpenApiSpex.Plug.SwaggerUI do path: "/api/openapi", default_model_expand_depth: 3, display_operation_id: true - on_complete: {MyAppWeb.RuntimeConfig, :on_complete} end # Other scopes may use custom stacks. @@ -37,6 +30,28 @@ defmodule OpenApiSpex.Plug.SwaggerUI do resources "/users", MyAppWeb.UserController, only: [:index, :create, :show] get "/openapi", OpenApiSpex.Plug.RenderSpec, :show end + + ## Runtime Configuration + + SwaggerUI also accepts functions as parameters, allowing runtime configurations. + Because of limitations of `router.ex` file, the functions can be defined only as: `{module, function_name}`. + + ## Example + + defmodule RuntimeConfig do + def on_complete do + "function() { ui.preauthorizeApiKey("bearer", "your generated token here"); }" + end + end + + scope "/" do + pipe_through :browser # Use the default browser stack + + get "/swaggerui", OpenApiSpex.Plug.SwaggerUI, + path: "/api/openapi", + on_complete: {RuntimeConfig, :on_complete} + end + """ @behaviour Plug