Skip to content

Commit 5b30e1f

Browse files
sauterldzikoysk
andauthored
GH-161 Check if WebJarHandler is already registered (#166)
* Resolved #161 by checking first if a webjarhandler is registered * Addressed PR comment and expanded unit test * GH-166 Code review --------- Co-authored-by: dzikoysk <[email protected]>
1 parent 553ef56 commit 5b30e1f

File tree

2 files changed

+111
-9
lines changed

2 files changed

+111
-9
lines changed

javalin-plugins/javalin-swagger-plugin/src/main/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPlugin.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.javalin.openapi.plugin.swagger
22

33
import io.javalin.Javalin
4+
import io.javalin.http.HandlerType
45
import io.javalin.plugin.Plugin
56
import io.javalin.security.RouteRole
67

@@ -61,14 +62,19 @@ open class SwaggerPlugin @JvmOverloads constructor(private val configuration: Sw
6162
customStylesheetFiles = configuration.customStylesheetFiles,
6263
customJavaScriptFiles = configuration.customJavaScriptFiles
6364
)
65+
/** Register handler for swagger ui */
66+
app.get(configuration.uiPath, swaggerHandler, *configuration.roles)
6467

65-
val swaggerWebJarHandler = SwaggerWebJarHandler(
66-
swaggerWebJarPath = configuration.webJarPath
67-
)
68-
69-
app
70-
.get(configuration.uiPath, swaggerHandler, *configuration.roles)
71-
.get("${configuration.webJarPath}/*", swaggerWebJarHandler, *configuration.roles)
68+
/** Register webjar handler if and only if there isn't already a [SwaggerWebJarHandler] at configured route */
69+
app.javalinServlet().matcher
70+
.findEntries(HandlerType.GET, "${configuration.webJarPath}/*")
71+
.takeIf { routes -> routes.none { it.handler is SwaggerWebJarHandler } }
72+
?.run {
73+
val swaggerWebJarHandler = SwaggerWebJarHandler(
74+
swaggerWebJarPath = configuration.webJarPath
75+
)
76+
app.get("${configuration.webJarPath}/*", swaggerWebJarHandler, *configuration.roles)
77+
}
7278
}
7379

74-
}
80+
}

javalin-plugins/javalin-swagger-plugin/src/test/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPluginTest.kt

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,100 @@ internal class SwaggerPluginTest {
6868
}
6969
}
7070

71-
}
71+
@Test
72+
fun `should not fail if second swagger plugin is registered`(){
73+
val swaggerConfiguration = SwaggerConfiguration();
74+
val otherConfiguration = ExampleSwaggerPlugin();
75+
Javalin.create{
76+
it.plugins.register(SwaggerPlugin(swaggerConfiguration))
77+
it.plugins.register(otherConfiguration)
78+
}
79+
.start(8080)
80+
.use{
81+
val javalinHost = "http://localhost:8080"
82+
val webjarJsRoute = "/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui-bundle.js"
83+
84+
val response = Unirest.get("$javalinHost/swagger")
85+
.asString()
86+
.body
87+
88+
assertThat(response).contains("""src="$webjarJsRoute"""")
89+
assertThat(response).contains("""url: '/openapi?v=test'""")
90+
assertThat(response).doesNotContain("""url: '/example-docs?v=test'""")
91+
92+
val resourceResponse = Unirest.get("$javalinHost$webjarJsRoute")
93+
.asString()
94+
.body
95+
96+
assertThat(resourceResponse).isNotBlank
97+
98+
val otherResponse = Unirest.get("$javalinHost/example-ui")
99+
.asString()
100+
.body
101+
102+
assertThat(otherResponse).contains("""url: '/example-docs?v=test'""")
103+
assertThat(otherResponse).doesNotContain("""url: '/openapi?v=test'""")
104+
}
105+
}
106+
107+
@Test
108+
fun `should not fail if second swagger plugin is registered with routes`(){
109+
val swaggerConfiguration = SwaggerConfiguration();
110+
val otherConfiguration = ExampleSwaggerPlugin();
111+
Javalin.create{
112+
it.plugins.register(SwaggerPlugin(swaggerConfiguration))
113+
it.plugins.register(otherConfiguration)
114+
}.get("/some/route/"){ ctx -> ctx.result("Hello World")}
115+
.start(8080)
116+
.use{
117+
val javalinHost = "http://localhost:8080"
118+
119+
val webjarCssRoute = "/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui.css"
120+
val webjarJsRoute = "/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui-bundle.js"
121+
val webjarJsStandaloneRoute = "/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui-standalone-preset.js"
122+
123+
val response = Unirest.get("$javalinHost/swagger")
124+
.asString()
125+
.body
126+
127+
assertThat(response).contains("""href="$webjarCssRoute"""")
128+
assertThat(response).contains("""src="$webjarJsRoute"""")
129+
assertThat(response).contains("""src="$webjarJsStandaloneRoute"""")
130+
assertThat(response).contains("""url: '/openapi?v=test'""")
131+
assertThat(response).doesNotContain("""url: '/example-docs?v=test'""")
132+
133+
var resourceResponse = Unirest.get("$javalinHost$webjarCssRoute")
134+
.asString()
135+
.body
136+
137+
assertThat(resourceResponse).isNotBlank
138+
139+
resourceResponse = Unirest.get("$javalinHost$webjarJsRoute")
140+
.asString()
141+
.body
142+
143+
assertThat(resourceResponse).isNotBlank
144+
145+
resourceResponse = Unirest.get("$javalinHost$webjarJsStandaloneRoute")
146+
.asString()
147+
.body
148+
149+
assertThat(resourceResponse).isNotBlank
150+
151+
val otherResponse = Unirest.get("$javalinHost/example-ui")
152+
.asString()
153+
.body
154+
155+
assertThat(otherResponse).contains("""url: '/example-docs?v=test'""")
156+
assertThat(otherResponse).doesNotContain("""url: '/openapi?v=test'""")
157+
}
158+
}
159+
160+
class ExampleSwaggerPlugin : SwaggerPlugin(
161+
SwaggerConfiguration().apply {
162+
this.documentationPath = "/example-docs"
163+
this.uiPath = "/example-ui"
164+
}
165+
)
166+
167+
}

0 commit comments

Comments
 (0)