Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/multiple swagger uis #162

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ class NULL_CLASS

/** Null string because annotations do not support null values */
const val NULL_STRING = "-- This string represents a null value and shouldn't be used --"
/** Value to use for auto-generate operationId */
const val AUTO_STRING = "-- Auto-generate operationId on the fly. If you see this message you are either inspecting via debugger or something went wrong --"

object OpenApiOperation {
/** Value to use for auto-generate operationId */
Expand Down