Skip to content

Commit

Permalink
Add deepLinking configuration variable to Swagger plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikMeyer committed Jan 15, 2025
1 parent 01abaa9 commit 8e1a2a9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
public final class io/ktor/server/plugins/swagger/SwaggerConfig {
public fun <init> ()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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
}</script>
</body>
</html>
""".trimIndent(),
response
)
}

@Test
fun testSwaggerAllowDeepLinking() = testApplication {
routing {
swaggerUI("swagger") {
deepLinking = true
}
}

val response = client.get("/swagger").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://unpkg.com/[email protected]/favicon-32x32.png" 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',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
Expand Down Expand Up @@ -77,6 +120,7 @@ class SwaggerTest {
window.ui = SwaggerUIBundle({
url: '/swagger/documentation.yaml',
dom_id: '#swagger-ui',
deepLinking: false,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
Expand All @@ -101,7 +145,7 @@ class SwaggerTest {

val response = client.get("/openapi/documentation.yaml")
val body = response.bodyAsText()
assertEquals("application/yaml", response.contentType().toString())
assertEquals("application/yaml; charset=UTF-8", response.contentType().toString())
assertEquals("hello:\n world".filter { it.isLetterOrDigit() }, body.filter { it.isLetterOrDigit() })
}

Expand Down Expand Up @@ -133,6 +177,7 @@ class SwaggerTest {
window.ui = SwaggerUIBundle({
url: '/swagger/documentation.yaml',
dom_id: '#swagger-ui',
deepLinking: false,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
Expand Down

0 comments on commit 8e1a2a9

Please sign in to comment.