From 94c94bd223e56242253c0b730205c68a31e545b3 Mon Sep 17 00:00:00 2001 From: Mariia Skripchenko <61115099+marychatte@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:12:01 +0100 Subject: [PATCH] KTOR-7893 Add favicon to Swagger UI (#4528) --- .../api/ktor-server-swagger.api | 2 + .../io/ktor/server/plugins/swagger/Swagger.kt | 7 ++- .../server/plugins/swagger/SwaggerConfig.kt | 7 ++- .../server/plugins/swagger/SwaggerTest.kt | 49 ++++++++++++++++++- 4 files changed, 62 insertions(+), 3 deletions(-) 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 66b98aec931..984357117c8 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,8 +1,10 @@ public final class io/ktor/server/plugins/swagger/SwaggerConfig { public fun ()V public final fun customStyle (Ljava/lang/String;)V + public final fun getFaviconLocation ()Ljava/lang/String; public final fun getPackageLocation ()Ljava/lang/String; public final fun getVersion ()Ljava/lang/String; + 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 5da6578617d..fd072c163e4 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.server.plugins.swagger @@ -86,6 +86,11 @@ public fun Route.swaggerUI( config.customStyle?.let { link(href = it, rel = "stylesheet") } + link( + href = config.faviconLocation, + rel = "icon", + type = "image/x-icon" + ) } body { div { id = "swagger-ui" } 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..3bb020086a2 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.server.plugins.swagger @@ -28,4 +28,9 @@ public class SwaggerConfig { * Swagger package location */ public var packageLocation: String = "https://unpkg.com/swagger-ui-dist" + + /** + * 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 473e20d9ff6..e1be3a61d0e 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 @@ -8,7 +8,8 @@ import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* import io.ktor.server.testing.* -import kotlin.test.* +import kotlin.test.Test +import kotlin.test.assertEquals class SwaggerTest { @Test @@ -25,6 +26,7 @@ class SwaggerTest { Swagger UI +
@@ -65,6 +67,7 @@ class SwaggerTest { Swagger UI +
@@ -101,4 +104,48 @@ class SwaggerTest { assertEquals("text/yaml; charset=UTF-8", response.contentType().toString()) assertEquals("hello:\n world".filter { it.isLetterOrDigit() }, body.filter { it.isLetterOrDigit() }) } + + @Test + fun testCustomFavicon() = testApplication { + routing { + swaggerUI("swagger") { + faviconLocation = "https://www.google.com/favicon.ico" + } + } + + val response = client.get("/swagger") { + parameter("docExpansion", "list") + }.bodyAsText() + assertEquals( + """ + + + + Swagger UI + + + + +
+ + + + + + + """.trimIndent(), + response + ) + } }