From 588d18f56a8c3406686b15180d76bb77ea8f471b Mon Sep 17 00:00:00 2001 From: Adam Stachowicz Date: Wed, 20 Nov 2024 19:24:53 +0100 Subject: [PATCH] Fix EnableTryItOut UI option (#5031) --- src/NSwag.AspNet.Owin/SwaggerUi/index.html | 21 ++------------------- src/NSwag.AspNetCore/SwaggerUi/index.html | 21 ++------------------- src/NSwag.AspNetCore/SwaggerUiSettings.cs | 11 +++++++---- 3 files changed, 11 insertions(+), 42 deletions(-) diff --git a/src/NSwag.AspNet.Owin/SwaggerUi/index.html b/src/NSwag.AspNet.Owin/SwaggerUi/index.html index 1e23b0e15a..ff9a97267d 100644 --- a/src/NSwag.AspNet.Owin/SwaggerUi/index.html +++ b/src/NSwag.AspNet.Owin/SwaggerUi/index.html @@ -27,26 +27,10 @@ } var urls = {Urls}; - const disableTryItOutPlugin = function() { - return { - statePlugins: { - spec: { - wrapSelectors: { - allowTryItOutFor: function() { - return function() { - return {EnableTryItOut}; - } - } - } - } - } - } - } - // Build a system var ui = SwaggerUIBundle({ url: url, - urls: urls, + urls: urls, validatorUrl: {ValidatorUrl}, oauth2RedirectUrl: {RedirectUrl}, @@ -58,8 +42,7 @@ SwaggerUIStandalonePreset ], plugins: [ - SwaggerUIBundle.plugins.DownloadUrl, - disableTryItOutPlugin + SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" }); diff --git a/src/NSwag.AspNetCore/SwaggerUi/index.html b/src/NSwag.AspNetCore/SwaggerUi/index.html index 1e23b0e15a..ff9a97267d 100644 --- a/src/NSwag.AspNetCore/SwaggerUi/index.html +++ b/src/NSwag.AspNetCore/SwaggerUi/index.html @@ -27,26 +27,10 @@ } var urls = {Urls}; - const disableTryItOutPlugin = function() { - return { - statePlugins: { - spec: { - wrapSelectors: { - allowTryItOutFor: function() { - return function() { - return {EnableTryItOut}; - } - } - } - } - } - } - } - // Build a system var ui = SwaggerUIBundle({ url: url, - urls: urls, + urls: urls, validatorUrl: {ValidatorUrl}, oauth2RedirectUrl: {RedirectUrl}, @@ -58,8 +42,7 @@ SwaggerUIStandalonePreset ], plugins: [ - SwaggerUIBundle.plugins.DownloadUrl, - disableTryItOutPlugin + SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" }); diff --git a/src/NSwag.AspNetCore/SwaggerUiSettings.cs b/src/NSwag.AspNetCore/SwaggerUiSettings.cs index ee51dbaad5..7b6a2e7d1e 100644 --- a/src/NSwag.AspNetCore/SwaggerUiSettings.cs +++ b/src/NSwag.AspNetCore/SwaggerUiSettings.cs @@ -53,7 +53,11 @@ public SwaggerUiSettings() public string ServerUrl { get; set; } = ""; /// Specifies whether the "Try it out" option is enabled in Swagger UI 3. - public bool EnableTryItOut { get; set; } = true; + public bool EnableTryItOut + { + get => (bool)AdditionalSettings["tryItOutEnabled"]; + set => AdditionalSettings["tryItOutEnabled"] = value; + } /// /// Gets or sets a title for the Swagger UI page. @@ -157,8 +161,8 @@ internal override async Task TransformHtmlAsync(string html, HttpRequest } } - var swaggerRoutes = SwaggerRoutesFactory != null ? - (await SwaggerRoutesFactory(request, cancellationToken)).ToList() : + var swaggerRoutes = SwaggerRoutesFactory != null ? + (await SwaggerRoutesFactory(request, cancellationToken)).ToList() : SwaggerRoutes; htmlBuilder.Replace("{Urls}", !swaggerRoutes.Any() @@ -172,7 +176,6 @@ internal override async Task TransformHtmlAsync(string html, HttpRequest htmlBuilder.Replace("{ValidatorUrl}", ValidateSpecification ? "undefined" : "null") .Replace("{AdditionalSettings}", GenerateAdditionalSettings(AdditionalSettings)) - .Replace("{EnableTryItOut}", EnableTryItOut.ToString().ToLower()) .Replace("{RedirectUrl}", string.IsNullOrEmpty(ServerUrl) ? "window.location.origin + \"" + TransformToExternalPath(Path, request) +