diff --git a/javalin-plugins/javalin-swagger-plugin/src/main/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPlugin.kt b/javalin-plugins/javalin-swagger-plugin/src/main/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPlugin.kt index b60a4ca..0c5b907 100644 --- a/javalin-plugins/javalin-swagger-plugin/src/main/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPlugin.kt +++ b/javalin-plugins/javalin-swagger-plugin/src/main/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPlugin.kt @@ -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 + } + } } -} \ No newline at end of file +} diff --git a/javalin-plugins/javalin-swagger-plugin/src/test/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPluginTest.kt b/javalin-plugins/javalin-swagger-plugin/src/test/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPluginTest.kt index df65b5a..3717a25 100644 --- a/javalin-plugins/javalin-swagger-plugin/src/test/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPluginTest.kt +++ b/javalin-plugins/javalin-swagger-plugin/src/test/kotlin/io/javalin/openapi/plugin/swagger/SwaggerPluginTest.kt @@ -48,4 +48,37 @@ internal class SwaggerPluginTest { } } -} \ No newline at end of file + @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" + } + ) +}