Skip to content

Commit 94c94bd

Browse files
marychatteosipxd
authored andcommitted
KTOR-7893 Add favicon to Swagger UI (#4528)
1 parent c0b9cb7 commit 94c94bd

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

ktor-server/ktor-server-plugins/ktor-server-swagger/api/ktor-server-swagger.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
public final class io/ktor/server/plugins/swagger/SwaggerConfig {
22
public fun <init> ()V
33
public final fun customStyle (Ljava/lang/String;)V
4+
public final fun getFaviconLocation ()Ljava/lang/String;
45
public final fun getPackageLocation ()Ljava/lang/String;
56
public final fun getVersion ()Ljava/lang/String;
7+
public final fun setFaviconLocation (Ljava/lang/String;)V
68
public final fun setPackageLocation (Ljava/lang/String;)V
79
public final fun setVersion (Ljava/lang/String;)V
810
}

ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/Swagger.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package io.ktor.server.plugins.swagger
@@ -86,6 +86,11 @@ public fun Route.swaggerUI(
8686
config.customStyle?.let {
8787
link(href = it, rel = "stylesheet")
8888
}
89+
link(
90+
href = config.faviconLocation,
91+
rel = "icon",
92+
type = "image/x-icon"
93+
)
8994
}
9095
body {
9196
div { id = "swagger-ui" }

ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/src/io/ktor/server/plugins/swagger/SwaggerConfig.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package io.ktor.server.plugins.swagger
@@ -28,4 +28,9 @@ public class SwaggerConfig {
2828
* Swagger package location
2929
*/
3030
public var packageLocation: String = "https://unpkg.com/swagger-ui-dist"
31+
32+
/**
33+
* Swagger favicon location
34+
*/
35+
public var faviconLocation: String = "https://unpkg.com/swagger-ui-dist@$version/favicon-32x32.png"
3136
}

ktor-server/ktor-server-plugins/ktor-server-swagger/jvm/test/io/ktor/server/plugins/swagger/SwaggerTest.kt

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import io.ktor.client.request.*
88
import io.ktor.client.statement.*
99
import io.ktor.http.*
1010
import io.ktor.server.testing.*
11-
import kotlin.test.*
11+
import kotlin.test.Test
12+
import kotlin.test.assertEquals
1213

1314
class SwaggerTest {
1415
@Test
@@ -25,6 +26,7 @@ class SwaggerTest {
2526
<head>
2627
<title>Swagger UI</title>
2728
<link href="https://unpkg.com/[email protected]/swagger-ui.css" rel="stylesheet">
29+
<link href="https://unpkg.com/[email protected]/favicon-32x32.png" rel="icon" type="image/x-icon">
2830
</head>
2931
<body>
3032
<div id="swagger-ui"></div>
@@ -65,6 +67,7 @@ class SwaggerTest {
6567
<head>
6668
<title>Swagger UI</title>
6769
<link href="https://unpkg.com/[email protected]/swagger-ui.css" rel="stylesheet">
70+
<link href="https://unpkg.com/[email protected]/favicon-32x32.png" rel="icon" type="image/x-icon">
6871
</head>
6972
<body>
7073
<div id="swagger-ui"></div>
@@ -101,4 +104,48 @@ class SwaggerTest {
101104
assertEquals("text/yaml; charset=UTF-8", response.contentType().toString())
102105
assertEquals("hello:\n world".filter { it.isLetterOrDigit() }, body.filter { it.isLetterOrDigit() })
103106
}
107+
108+
@Test
109+
fun testCustomFavicon() = testApplication {
110+
routing {
111+
swaggerUI("swagger") {
112+
faviconLocation = "https://www.google.com/favicon.ico"
113+
}
114+
}
115+
116+
val response = client.get("/swagger") {
117+
parameter("docExpansion", "list")
118+
}.bodyAsText()
119+
assertEquals(
120+
"""
121+
<!DOCTYPE html>
122+
<html>
123+
<head>
124+
<title>Swagger UI</title>
125+
<link href="https://unpkg.com/[email protected]/swagger-ui.css" rel="stylesheet">
126+
<link href="https://www.google.com/favicon.ico" rel="icon" type="image/x-icon">
127+
</head>
128+
<body>
129+
<div id="swagger-ui"></div>
130+
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin="anonymous"></script>
131+
<script src="https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js" crossorigin="anonymous"></script>
132+
<script>window.onload = function() {
133+
window.ui = SwaggerUIBundle({
134+
url: '/swagger/documentation.yaml',
135+
dom_id: '#swagger-ui',
136+
presets: [
137+
SwaggerUIBundle.presets.apis,
138+
SwaggerUIStandalonePreset
139+
],
140+
layout: 'StandaloneLayout',
141+
docExpansion: 'list'
142+
});
143+
}</script>
144+
</body>
145+
</html>
146+
147+
""".trimIndent(),
148+
response
149+
)
150+
}
104151
}

0 commit comments

Comments
 (0)