-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sergey Okatov <sokatov@gmail.com>
- Loading branch information
Showing
15 changed files
with
1,291 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,30 @@ | ||
[versions] | ||
kotlin = "2.1.0" | ||
|
||
kotlinx-datetime = "0.6.1" | ||
kotlinx-serialization = "1.6.3" | ||
|
||
binaryCompabilityValidator = "0.13.2" | ||
|
||
openapi-generator = "7.3.0" | ||
jackson = "2.16.1" | ||
# BASE | ||
jvm-compiler = "17" | ||
jvm-language = "21" | ||
|
||
[libraries] | ||
plugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } | ||
plugin-binaryCompatibilityValidator = "org.jetbrains.kotlinx:binary-compatibility-validator:0.13.2" | ||
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version = "0.6.1" } | ||
plugin-binaryCompatibilityValidator = { module = "org.jetbrains.kotlinx:binary-compatibility-validator", version.ref = "binaryCompabilityValidator" } | ||
kotlinx-datetime = {module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime"} | ||
kotlinx-serialization-core = {module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization"} | ||
kotlinx-serialization-json = {module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization"} | ||
|
||
jackson-kotlin = {module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson"} | ||
jackson-datatype = {module = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310", version.ref = "jackson"} | ||
|
||
[plugins] | ||
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } | ||
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } | ||
kotlin-multiplatform = {id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin"} | ||
kotlin-jvm = {id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin"} | ||
openapi-generator = {id = "org.openapi.generator", version.ref = "openapi-generator"} | ||
crowdproj-generator = {id = "com.crowdproj.generator", version = "0.2.0"} | ||
kotlinx-serialization = {id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
ok-marketplace-be/ok-marketplace-api-v1-jackson/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
plugins { | ||
id("build-jvm") | ||
alias(libs.plugins.openapi.generator) | ||
} | ||
|
||
sourceSets { | ||
main { | ||
java.srcDir(layout.buildDirectory.dir("generate-resources/main/src/main/kotlin")) | ||
} | ||
} | ||
|
||
/** | ||
* Настраиваем генерацию здесь | ||
*/ | ||
openApiGenerate { | ||
val openapiGroup = "${rootProject.group}.api.v1" | ||
generatorName.set("kotlin") // Это и есть активный генератор | ||
packageName.set(openapiGroup) | ||
apiPackage.set("$openapiGroup.api") | ||
modelPackage.set("$openapiGroup.models") | ||
invokerPackage.set("$openapiGroup.invoker") | ||
// inputSpec.set("$specDir/specs-ad-v1.yaml") | ||
inputSpec.set(rootProject.ext["spec-v1"] as String) | ||
|
||
/** | ||
* Здесь указываем, что нам нужны только модели, все остальное не нужно | ||
* https://openapi-generator.tech/docs/globals | ||
*/ | ||
globalProperties.apply { | ||
put("models", "") | ||
put("modelDocs", "false") | ||
} | ||
|
||
/** | ||
* Настройка дополнительных параметров из документации по генератору | ||
* https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/kotlin.md | ||
*/ | ||
configOptions.set( | ||
mapOf( | ||
"dateLibrary" to "string", | ||
"enumPropertyNaming" to "UPPERCASE", | ||
"serializationLibrary" to "jackson", | ||
"collectionType" to "list" | ||
) | ||
) | ||
} | ||
|
||
dependencies { | ||
implementation(kotlin("stdlib")) | ||
implementation(libs.jackson.kotlin) | ||
implementation(libs.jackson.datatype) | ||
testImplementation(kotlin("test-junit")) | ||
} | ||
|
||
tasks { | ||
compileKotlin { | ||
dependsOn(openApiGenerate) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
ok-marketplace-be/ok-marketplace-api-v1-jackson/src/main/kotlin/ApiV1Mapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ru.otus.otuskotlin.marketplace.api.v1 | ||
|
||
import com.fasterxml.jackson.databind.MapperFeature | ||
import com.fasterxml.jackson.databind.json.JsonMapper | ||
import ru.otus.otuskotlin.marketplace.api.v1.models.IRequest | ||
import ru.otus.otuskotlin.marketplace.api.v1.models.IResponse | ||
val apiV1Mapper = JsonMapper.builder().run { | ||
// configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
enable(MapperFeature.USE_BASE_TYPE_AS_DEFAULT_IMPL) | ||
// setSerializationInclusion(JsonInclude.Include.NON_NULL) | ||
build() | ||
} | ||
|
||
@Suppress("unused") | ||
fun apiV1RequestSerialize(request: IRequest): String = apiV1Mapper.writeValueAsString(request) | ||
|
||
@Suppress("UNCHECKED_CAST", "unused") | ||
fun <T : IRequest> apiV1RequestDeserialize(json: String): T = | ||
apiV1Mapper.readValue(json, IRequest::class.java) as T | ||
|
||
@Suppress("unused") | ||
fun apiV1ResponseSerialize(response: IResponse): String = apiV1Mapper.writeValueAsString(response) | ||
|
||
@Suppress("UNCHECKED_CAST", "unused") | ||
fun <T : IResponse> apiV1ResponseDeserialize(json: String): T = | ||
apiV1Mapper.readValue(json, IResponse::class.java) as T |
49 changes: 49 additions & 0 deletions
49
...arketplace-be/ok-marketplace-api-v1-jackson/src/test/kotlin/RequestV1SerializationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ru.otus.otuskotlin.marketplace.api.v1 | ||
|
||
import ru.otus.otuskotlin.marketplace.api.v1.models.* | ||
import kotlin.test.Test | ||
import kotlin.test.assertContains | ||
import kotlin.test.assertEquals | ||
|
||
class RequestV1SerializationTest { | ||
private val request = AdCreateRequest( | ||
debug = AdDebug( | ||
mode = AdRequestDebugMode.STUB, | ||
stub = AdRequestDebugStubs.BAD_TITLE | ||
), | ||
ad = AdCreateObject( | ||
title = "ad title", | ||
description = "ad description", | ||
adType = DealSide.DEMAND, | ||
visibility = AdVisibility.PUBLIC, | ||
) | ||
) | ||
|
||
@Test | ||
fun serialize() { | ||
val json = apiV1Mapper.writeValueAsString(request) | ||
|
||
assertContains(json, Regex("\"title\":\\s*\"ad title\"")) | ||
assertContains(json, Regex("\"mode\":\\s*\"stub\"")) | ||
assertContains(json, Regex("\"stub\":\\s*\"badTitle\"")) | ||
assertContains(json, Regex("\"requestType\":\\s*\"create\"")) | ||
} | ||
|
||
@Test | ||
fun deserialize() { | ||
val json = apiV1Mapper.writeValueAsString(request) | ||
val obj = apiV1Mapper.readValue(json, IRequest::class.java) as AdCreateRequest | ||
|
||
assertEquals(request, obj) | ||
} | ||
|
||
@Test | ||
fun deserializeNaked() { | ||
val jsonString = """ | ||
{"ad": null} | ||
""".trimIndent() | ||
val obj = apiV1Mapper.readValue(jsonString, AdCreateRequest::class.java) | ||
|
||
assertEquals(null, obj.ad) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...rketplace-be/ok-marketplace-api-v1-jackson/src/test/kotlin/ResponseV1SerializationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ru.otus.otuskotlin.marketplace.api.v1 | ||
|
||
import ru.otus.otuskotlin.marketplace.api.v1.models.* | ||
import kotlin.test.Test | ||
import kotlin.test.assertContains | ||
import kotlin.test.assertEquals | ||
|
||
class ResponseV1SerializationTest { | ||
private val response = AdCreateResponse( | ||
ad = AdResponseObject( | ||
title = "ad title", | ||
description = "ad description", | ||
adType = DealSide.DEMAND, | ||
visibility = AdVisibility.PUBLIC, | ||
) | ||
) | ||
|
||
@Test | ||
fun serialize() { | ||
val json = apiV1Mapper.writeValueAsString(response) | ||
|
||
assertContains(json, Regex("\"title\":\\s*\"ad title\"")) | ||
assertContains(json, Regex("\"responseType\":\\s*\"create\"")) | ||
} | ||
|
||
@Test | ||
fun deserialize() { | ||
val json = apiV1Mapper.writeValueAsString(response) | ||
val obj = apiV1Mapper.readValue(json, IResponse::class.java) as AdCreateResponse | ||
|
||
assertEquals(response, obj) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
ok-marketplace-be/ok-marketplace-api-v2-kmp/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import org.openapitools.generator.gradle.plugin.tasks.GenerateTask | ||
|
||
plugins { | ||
id("build-kmp") | ||
alias(libs.plugins.crowdproj.generator) | ||
alias(libs.plugins.kotlinx.serialization) | ||
} | ||
|
||
crowdprojGenerate { | ||
packageName.set("${project.group}.api.v2") | ||
inputSpec.set(rootProject.ext["spec-v2"] as String) | ||
} | ||
|
||
kotlin { | ||
sourceSets { | ||
val serializationVersion: String by project | ||
val commonMain by getting { | ||
kotlin.srcDirs(layout.buildDirectory.dir("generate-resources/src/commonMain/kotlin")) | ||
dependencies { | ||
implementation(kotlin("stdlib-common")) | ||
|
||
implementation(libs.kotlinx.serialization.core) | ||
implementation(libs.kotlinx.serialization.json) | ||
} | ||
} | ||
val commonTest by getting { | ||
dependencies { | ||
implementation(kotlin("test-common")) | ||
implementation(kotlin("test-annotations-common")) | ||
} | ||
} | ||
val jvmTest by getting { | ||
dependencies { | ||
implementation(kotlin("test-junit")) | ||
} | ||
} | ||
} | ||
} | ||
|
||
tasks { | ||
val openApiGenerateTask: GenerateTask = getByName("openApiGenerate", GenerateTask::class) { | ||
outputDir.set(layout.buildDirectory.file("generate-resources").get().toString()) | ||
finalizedBy("compileCommonMainKotlinMetadata") | ||
} | ||
filter { it.name.startsWith("compile") }.forEach { | ||
it.dependsOn(openApiGenerateTask) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
ok-marketplace-be/ok-marketplace-api-v2-kmp/src/commonMain/kotlin/MkplAdApiSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@file:Suppress("unused") | ||
|
||
package ru.otus.otuskotlin.marketplace.api.v2 | ||
|
||
import kotlinx.serialization.json.Json | ||
import ru.otus.otuskotlin.marketplace.api.v2.models.IRequest | ||
import ru.otus.otuskotlin.marketplace.api.v2.models.IResponse | ||
|
||
@Suppress("JSON_FORMAT_REDUNDANT_DEFAULT") | ||
val apiV2Mapper = Json { | ||
// ignoreUnknownKeys = true | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T : IRequest> apiV2RequestDeserialize(json: String) = | ||
apiV2Mapper.decodeFromString<IRequest>(json) as T | ||
|
||
fun apiV2ResponseSerialize(obj: IResponse): String = | ||
apiV2Mapper.encodeToString(IResponse.serializer(), obj) | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T : IResponse> apiV2ResponseDeserialize(json: String) = | ||
apiV2Mapper.decodeFromString<IResponse>(json) as T | ||
|
||
@Suppress("unused") | ||
fun apiV2RequestSerialize(obj: IRequest): String = | ||
apiV2Mapper.encodeToString(IRequest.serializer(), obj) |
Oops, something went wrong.