diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/util/DocumentationDsl.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/util/DocumentationDsl.kt index 8d0d124fa..21ba7622e 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/server/util/DocumentationDsl.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/util/DocumentationDsl.kt @@ -12,23 +12,44 @@ fun getSimpleParamItem(ctx: Context, param: Param): String? { is Param.FormParam -> ctx.formParam(param.key) is Param.PathParam -> ctx.pathParam(param.key) is Param.QueryParam -> ctx.queryParam(param.key) + else -> throw IllegalStateException("Invalid param") } } @Suppress("UNCHECKED_CAST") fun getParam(ctx: Context, param: Param): T { - val typedItem: Any? = when (param.clazz) { - String::class.java -> getSimpleParamItem(ctx, param) - Int::class.java -> getSimpleParamItem(ctx, param)?.toIntOrNull() - Long::class.java -> getSimpleParamItem(ctx, param)?.toLongOrNull() - Boolean::class.java -> getSimpleParamItem(ctx, param)?.toBoolean() - Float::class.java -> getSimpleParamItem(ctx, param)?.toFloatOrNull() - Double::class.java -> getSimpleParamItem(ctx, param)?.toDoubleOrNull() + if (param is Param.QueryParams<*, *>) { + val item = ctx.queryParams(param.key).filter(String::isNotBlank) + val typedItem: List = when (param.clazz) { + String::class.java, java.lang.String::class.java -> item + Int::class.java, java.lang.Integer::class.java -> item.map { it.toIntOrNull() } + Long::class.java, java.lang.Long::class.java -> item.map { it.toLongOrNull() } + Boolean::class.java, java.lang.Boolean::class.java -> item.map { it.toBoolean() } + Float::class.java, java.lang.Float::class.java -> item.map { it.toFloatOrNull() } + Double::class.java, java.lang.Double::class.java -> item.map { it.toDoubleOrNull() } + else -> throw IllegalStateException("Unknown class ${param.clazz.simpleName}") + }.let { + if (param.nullable) { + it + } else { + it.filterNotNull() + } + }.ifEmpty { param.defaultValue } + return typedItem as T + } + val typedItem: Any? = when (val clazz = param.clazz as Class) { + String::class.java, java.lang.String::class.java -> getSimpleParamItem(ctx, param) ?: param.defaultValue + Int::class.java, java.lang.Integer::class.java -> getSimpleParamItem(ctx, param)?.toIntOrNull() ?: param.defaultValue + Long::class.java, java.lang.Long::class.java -> getSimpleParamItem(ctx, param)?.toLongOrNull() ?: param.defaultValue + Boolean::class.java, java.lang.Boolean::class.java -> getSimpleParamItem(ctx, param)?.toBoolean() ?: param.defaultValue + Float::class.java, java.lang.Float::class.java -> getSimpleParamItem(ctx, param)?.toFloatOrNull() ?: param.defaultValue + Double::class.java, java.lang.Double::class.java -> getSimpleParamItem(ctx, param)?.toDoubleOrNull() ?: param.defaultValue else -> { when (param) { - is Param.FormParam -> ctx.formParamAsClass(param.key, param.clazz) - is Param.PathParam -> ctx.pathParamAsClass(param.key, param.clazz) - is Param.QueryParam -> ctx.queryParamAsClass(param.key, param.clazz) + is Param.FormParam -> ctx.formParamAsClass(param.key, clazz) + is Param.PathParam -> ctx.pathParamAsClass(param.key, clazz) + is Param.QueryParam -> ctx.queryParamAsClass(param.key, clazz) + else -> throw IllegalStateException("Invalid param") }.let { if (param.nullable) { it.allowNullable().get() ?: param.defaultValue @@ -61,7 +82,8 @@ inline fun getDocumentation( when (it) { is Param.FormParam -> formParam(it.key, it.clazz, !it.nullable && it.defaultValue == null) is Param.PathParam -> pathParam(it.key, it.clazz) - is Param.QueryParam -> queryParam(it.key, it.clazz,) + is Param.QueryParam -> queryParam(it.key, it.clazz) + is Param.QueryParams<*, *> -> queryParam(it.key, it.clazz, isRepeatable = true) } } } @@ -83,30 +105,39 @@ inline fun formParam(key: String, defaultValue: T? = null): Param.Fo inline fun queryParam(key: String, defaultValue: T? = null): Param.QueryParam { return Param.QueryParam(key, T::class.java, defaultValue, null is T) } +inline fun queryParams(key: String, defaultValue: List = emptyList()): Param.QueryParams> { + return Param.QueryParams(key, T::class.java, defaultValue, null is T) +} inline fun pathParam(key: String): Param.PathParam { return Param.PathParam(key, T::class.java, null, false) } sealed class Param { abstract val key: String - abstract val clazz: Class + abstract val clazz: Class<*> abstract val defaultValue: T? abstract val nullable: Boolean data class FormParam( override val key: String, - override val clazz: Class, + override val clazz: Class<*>, override val defaultValue: T?, override val nullable: Boolean ) : Param() data class QueryParam( override val key: String, - override val clazz: Class, + override val clazz: Class<*>, override val defaultValue: T?, override val nullable: Boolean ) : Param() + data class QueryParams>( + override val key: String, + override val clazz: Class, + override val defaultValue: T, + override val nullable: Boolean + ) : Param() data class PathParam( override val key: String, - override val clazz: Class, + override val clazz: Class<*>, override val defaultValue: T?, override val nullable: Boolean ) : Param()