Skip to content

Commit

Permalink
Resolved javalin#161 by checking first if a webjarhandler is registered
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterl committed Feb 22, 2023
1 parent 67da48d commit 958185b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.javalin.openapi.plugin.swagger

import io.javalin.Javalin
import io.javalin.http.HandlerType
import io.javalin.http.servlet.getBasicAuthCredentials
import io.javalin.plugin.Plugin
import io.javalin.security.RouteRole
Expand Down Expand Up @@ -35,14 +36,17 @@ open class SwaggerPlugin @JvmOverloads constructor(private val configuration: Sw
routingPath = app.cfg.routing.contextPath,
basePath = configuration.basePath
)
/** Register handler for swagger ui */
app.get(configuration.uiPath, swaggerHandler, *configuration.roles)

val swaggerWebJarHandler = SwaggerWebJarHandler(
swaggerWebJarPath = configuration.webJarPath
)

app
.get(configuration.uiPath, swaggerHandler, *configuration.roles)
.get("${configuration.webJarPath}/*", swaggerWebJarHandler, *configuration.roles)
/** Register webjar handler if and only if there isn't already a [SwaggerWebJarHandler] at configured route */
val routes = app.javalinServlet().matcher.findEntries(HandlerType.GET, "${configuration.webJarPath}/*")
if(routes.isNotEmpty() && routes.first().handler !is SwaggerWebJarHandler){
val swaggerWebJarHandler = SwaggerWebJarHandler(
swaggerWebJarPath = configuration.webJarPath
)
app.get("${configuration.webJarPath}/*", swaggerWebJarHandler, *configuration.roles)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,64 @@ internal class SwaggerPluginTest {
}
}

}
@Test
fun `should not fail if second swagger plugin is registered`(){
val swaggerConfiguration = SwaggerConfiguration();
val otherConfiguration = ExampleSwaggerPlugin();
Javalin.create{
it.plugins.register(SwaggerPlugin(swaggerConfiguration))
it.plugins.register(otherConfiguration)
}
.start(8080)
.use{
val javalinHost = "http://localhost:8080"

val webjarCssRoute = "/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui.css"
val webjarJsRoute = "/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui-bundle.js"
val webjarJsStandaloneRoute = "/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui-standalone-preset.js"

val response = Unirest.get("$javalinHost/swagger")
.asString()
.body

assertThat(response).contains("""href="$webjarCssRoute"""")
assertThat(response).contains("""src="$webjarJsRoute"""")
assertThat(response).contains("""src="$webjarJsStandaloneRoute"""")
assertThat(response).contains("""url: '/openapi?v=test'""")
assertThat(response).doesNotContain("""url: '/example-docs?v=test'""")

var resourceResponse = Unirest.get("$javalinHost$webjarCssRoute")
.asString()
.body

assertThat(resourceResponse).isNotBlank

resourceResponse = Unirest.get("$javalinHost$webjarJsRoute")
.asString()
.body

assertThat(resourceResponse).isNotBlank

resourceResponse = Unirest.get("$javalinHost$webjarJsStandaloneRoute")
.asString()
.body

assertThat(resourceResponse).isNotBlank

val otherResponse = Unirest.get("$javalinHost/example-ui")
.asString()
.body

assertThat(otherResponse).contains("""url: '/example-docs?v=test'""")
assertThat(otherResponse).doesNotContain("""url: '/openapi?v=test'""")
}
}

class ExampleSwaggerPlugin : SwaggerPlugin(
SwaggerConfiguration().apply {
this.documentationPath = "/example-docs"
this.uiPath = "/example-ui"
}
)

}

0 comments on commit 958185b

Please sign in to comment.