From 7f0f9042c7d28ea2518e20c17c46bbb3f7d76f84 Mon Sep 17 00:00:00 2001 From: Fredrik Meyer Date: Tue, 17 Dec 2024 16:08:44 +0100 Subject: [PATCH] Add deepLinking configuration variable to Swagger plugin --- .../io/ktor/server/plugins/swagger/Swagger.kt | 1 + .../server/plugins/swagger/SwaggerConfig.kt | 7 +++ .../server/plugins/swagger/SwaggerTest.kt | 43 +++++++++++++++++++ 3 files changed, 51 insertions(+) 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 5da6578617d..8b23331e726 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 @@ -105,6 +105,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 a69ced719d3..afcfe91f1dd 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 @@ -28,4 +28,11 @@ public class SwaggerConfig { * Swagger package location */ 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 } 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 473e20d9ff6..0512228ede5 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 @@ -34,6 +34,48 @@ 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 + + + +
+ + +