From 82891b5410943ff5ce57e63a2270cebbc64236be Mon Sep 17 00:00:00 2001 From: Shiina Kin Date: Sat, 12 Oct 2024 17:56:22 +0800 Subject: [PATCH] feat: manually specify sealed class Request parameters --- app/build.gradle.kts | 1 + .../controller/StrategyController.kt | 66 +++++++++++++++++-- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 048d3f07..a061e5ce 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -45,6 +45,7 @@ dependencies { implementation("io.ktor:ktor-server-default-headers-jvm") implementation("io.ktor:ktor-server-forwarded-header-jvm") + implementation("io.swagger.parser.v3:swagger-parser:2.1.21") implementation("io.github.smiley4:ktor-swagger-ui:3.5.0") implementation("com.ucasoft.ktor:ktor-simple-cache-jvm:$ktorSimpleCacheVersion") diff --git a/app/src/main/kotlin/io/sakurasou/controller/StrategyController.kt b/app/src/main/kotlin/io/sakurasou/controller/StrategyController.kt index 43c580f3..085ff8d8 100644 --- a/app/src/main/kotlin/io/sakurasou/controller/StrategyController.kt +++ b/app/src/main/kotlin/io/sakurasou/controller/StrategyController.kt @@ -1,10 +1,6 @@ package io.sakurasou.controller -import io.github.smiley4.ktorswaggerui.dsl.routing.delete -import io.github.smiley4.ktorswaggerui.dsl.routing.get -import io.github.smiley4.ktorswaggerui.dsl.routing.patch -import io.github.smiley4.ktorswaggerui.dsl.routing.post -import io.github.smiley4.ktorswaggerui.dsl.routing.route +import io.github.smiley4.ktorswaggerui.dsl.routing.* import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.request.* @@ -23,8 +19,12 @@ import io.sakurasou.controller.vo.StrategyVO import io.sakurasou.extension.id import io.sakurasou.extension.pageRequest import io.sakurasou.extension.success +import io.sakurasou.model.strategy.LocalStrategy +import io.sakurasou.model.strategy.S3Strategy +import io.sakurasou.model.strategy.StrategyConfig import io.sakurasou.plugins.AuthorizationPlugin import io.sakurasou.service.strategy.StrategyService +import io.swagger.v3.oas.models.media.Schema /** * @author Shiina Kin @@ -68,8 +68,60 @@ private fun Route.insertStrategy(controller: StrategyController) { post({ protected = true request { - body { - description = "strategy request" + body(Schema().apply { + addProperty("config", Schema().apply { + description = "io/sakurasou/model/strategy/StrategyConfig.kt" + oneOf( + listOf( + Schema().apply { + description = "Local strategy configuration" + addProperty("uploadFolder", Schema().apply { + type = "string" + }) + addProperty("strategyType", Schema().apply { + type = "string" + }) + addProperty("type", Schema().apply { + type = "string" + description = "same to strategyType" + }) + }, + Schema().apply { + description = "S3 strategy configuration" + addProperty("endpoint", Schema().apply { + type = "string" + }) + addProperty("bucketName", Schema().apply { + type = "string" + }) + addProperty("region", Schema().apply { + type = "string" + }) + addProperty("accessKey", Schema().apply { + type = "string" + }) + addProperty("secretKey", Schema().apply { + type = "string" + }) + addProperty("publicUrl", Schema().apply { + type = "string" + }) + addProperty("strategyType", Schema().apply { + type = "string" + }) + addProperty("type", Schema().apply { + type = "string" + description = "same to strategyType" + }) + } + ) + ) + }) + addProperty("name", Schema().apply { + type = "string" + }) + }) { + description = "`config` need a extra field `type`, same to strategyType" required = true } }