Skip to content

Commit

Permalink
Resolved javalin#161 by carefully ignoring that another webjar handle…
Browse files Browse the repository at this point in the history
…r already exists
  • Loading branch information
sauterl committed Feb 20, 2023
1 parent b49592f commit b7b0e64
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,24 @@ open class SwaggerPlugin @JvmOverloads constructor(private val configuration: Sw
basePath = configuration.basePath
)

/* Better even would be to check here if the path is already registered in javalin */

val swaggerWebJarHandler = SwaggerWebJarHandler(
swaggerWebJarPath = configuration.webJarPath
)

app
.get(configuration.uiPath, swaggerHandler, *configuration.roles)
.get("${configuration.webJarPath}/*", swaggerWebJarHandler, *configuration.roles)
try {
app.get("${configuration.webJarPath}/*", swaggerWebJarHandler, *configuration.roles)
}catch(ex: java.lang.IllegalArgumentException){
/* If there is any other exception than the one that this GET handler for this webJarPath already exists, we care.
* Otherwise we can ignore it, as the main goal -- to serve the webjar -- is already achieved by some other configuration.
*/
if(ex.message?.contains("type='GET'") != true || ex.message?.contains("path='${configuration.webJarPath}") != true){
throw ex
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,37 @@ 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 response = Unirest.get("http://localhost:8080/swagger")
.asString()
.body

assertThat(response).contains("""href="/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui.css"""")
assertThat(response).contains("""src="/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui-bundle.js"""")
assertThat(response).contains("""src="/webjars/swagger-ui/${swaggerConfiguration.version}/swagger-ui-standalone-preset.js"""")
assertThat(response).contains("""url: '/openapi?v=test'""")

val otherResponse = Unirest.get("http://localhost:8080/example-ui")
.asString()
.body

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

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

0 comments on commit b7b0e64

Please sign in to comment.