From 8e1a2a93c4680c503e0353d1c7ac6f57e29ffb84 Mon Sep 17 00:00:00 2001 From: Fredrik Meyer Date: Wed, 15 Jan 2025 11:25:51 +0100 Subject: [PATCH] Add deepLinking configuration variable to Swagger plugin --- .../api/ktor-server-swagger.api | 2 + .../io/ktor/server/plugins/swagger/Swagger.kt | 1 + .../server/plugins/swagger/SwaggerConfig.kt | 7 +++ .../server/plugins/swagger/SwaggerTest.kt | 47 ++++++++++++++++++- 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/ktor-server/ktor-server-plugins/ktor-server-swagger/api/ktor-server-swagger.api b/ktor-server/ktor-server-plugins/ktor-server-swagger/api/ktor-server-swagger.api index 984357117c..582437dc79 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-swagger/api/ktor-server-swagger.api +++ b/ktor-server/ktor-server-plugins/ktor-server-swagger/api/ktor-server-swagger.api @@ -1,9 +1,11 @@ public final class io/ktor/server/plugins/swagger/SwaggerConfig { public fun ()V public final fun customStyle (Ljava/lang/String;)V + public final fun getDeepLinking ()Z public final fun getFaviconLocation ()Ljava/lang/String; public final fun getPackageLocation ()Ljava/lang/String; public final fun getVersion ()Ljava/lang/String; + public final fun setDeepLinking (Z)V public final fun setFaviconLocation (Ljava/lang/String;)V public final fun setPackageLocation (Ljava/lang/String;)V public final fun setVersion (Ljava/lang/String;)V diff --git a/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/Swagger.kt b/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/Swagger.kt index fd072c163e..902b11b19c 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/Swagger.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/Swagger.kt @@ -110,6 +110,7 @@ window.onload = function() { window.ui = SwaggerUIBundle({ url: '$fullPath/$apiUrl', dom_id: '#swagger-ui', + deepLinking: ${config.deepLinking}, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset diff --git a/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/SwaggerConfig.kt b/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/SwaggerConfig.kt index 3bb020086a..bbf5070add 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/SwaggerConfig.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/SwaggerConfig.kt @@ -30,6 +30,13 @@ public class SwaggerConfig { public var packageLocation: String = "https://unpkg.com/swagger-ui-dist" /** + * Whether to allow deep linking in Swagger UI, as described here: https://swagger.io/docs/open-source-tools/swagger-ui/usage/deep-linking/ + * + * Defaults to `false`. + */ + public var deepLinking: Boolean = false + + /* * Swagger favicon location */ public var faviconLocation: String = "https://unpkg.com/swagger-ui-dist@$version/favicon-32x32.png" diff --git a/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/test/io/ktor/server/plugins/swagger/SwaggerTest.kt b/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/test/io/ktor/server/plugins/swagger/SwaggerTest.kt index e8a451eba8..5951d97b41 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/test/io/ktor/server/plugins/swagger/SwaggerTest.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/test/io/ktor/server/plugins/swagger/SwaggerTest.kt @@ -36,6 +36,49 @@ class SwaggerTest { window.ui = SwaggerUIBundle({ url: '/swagger/documentation.yaml', dom_id: '#swagger-ui', + deepLinking: false, + presets: [ + SwaggerUIBundle.presets.apis, + SwaggerUIStandalonePreset + ], + layout: 'StandaloneLayout' + }); + } + + + + """.trimIndent(), + response + ) + } + + @Test + fun testSwaggerAllowDeepLinking() = testApplication { + routing { + swaggerUI("swagger") { + deepLinking = true + } + } + + val response = client.get("/swagger").bodyAsText() + assertEquals( + """ + + + + Swagger UI + + + + +
+ + +