Skip to content

Commit

Permalink
KTOR-7893 Add favicon to Swagger UI (#4528)
Browse files Browse the repository at this point in the history
  • Loading branch information
marychatte authored and osipxd committed Dec 12, 2024
1 parent ec1071d commit f6e4926
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
public final class io/ktor/server/plugins/swagger/SwaggerConfig {
public fun <init> ()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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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" }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +26,7 @@ class SwaggerTest {
<head>
<title>Swagger UI</title>
<link href="https://unpkg.com/[email protected]/swagger-ui.css" rel="stylesheet">
<link href="https://unpkg.com/[email protected]/favicon-32x32.png" rel="icon" type="image/x-icon">
</head>
<body>
<div id="swagger-ui"></div>
Expand Down Expand Up @@ -65,6 +67,7 @@ class SwaggerTest {
<head>
<title>Swagger UI</title>
<link href="https://unpkg.com/[email protected]/swagger-ui.css" rel="stylesheet">
<link href="https://unpkg.com/[email protected]/favicon-32x32.png" rel="icon" type="image/x-icon">
</head>
<body>
<div id="swagger-ui"></div>
Expand Down Expand Up @@ -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(
"""
<!DOCTYPE html>
<html>
<head>
<title>Swagger UI</title>
<link href="https://unpkg.com/[email protected]/swagger-ui.css" rel="stylesheet">
<link href="https://www.google.com/favicon.ico" rel="icon" type="image/x-icon">
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js" crossorigin="anonymous"></script>
<script>window.onload = function() {
window.ui = SwaggerUIBundle({
url: '/swagger/documentation.yaml',
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: 'StandaloneLayout',
docExpansion: 'list'
});
}</script>
</body>
</html>
""".trimIndent(),
response
)
}
}

0 comments on commit f6e4926

Please sign in to comment.