From e8e0cf4fcc8b54e5fb3059ba2cf9bca9f08ac21e Mon Sep 17 00:00:00 2001 From: lorenzsimon Date: Mon, 10 Apr 2023 11:59:50 +0200 Subject: [PATCH] feat - Add Kotlin module to model resolver --- kotlin-asyncapi-spring-web/pom.xml | 4 ++++ .../context/annotation/processor/ConverterUtils.kt | 9 +++++++++ .../context/annotation/processor/MessageProcessor.kt | 4 +--- .../context/annotation/processor/SchemaProcessor.kt | 4 +--- .../annotation/processor/SchemaProcessorTest.kt | 2 +- .../resources/annotation/annotation_integration.json | 4 +++- .../test/resources/annotation/message_component.json | 3 ++- .../test/resources/annotation/schema_component.json | 12 +++++++----- pom.xml | 5 +++++ 9 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/ConverterUtils.kt diff --git a/kotlin-asyncapi-spring-web/pom.xml b/kotlin-asyncapi-spring-web/pom.xml index fe6647b..646673c 100644 --- a/kotlin-asyncapi-spring-web/pom.xml +++ b/kotlin-asyncapi-spring-web/pom.xml @@ -31,6 +31,10 @@ com.fasterxml.jackson.core jackson-databind + + com.fasterxml.jackson.module + jackson-module-kotlin + io.swagger.core.v3 swagger-core diff --git a/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/ConverterUtils.kt b/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/ConverterUtils.kt new file mode 100644 index 0000000..0a6fa47 --- /dev/null +++ b/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/ConverterUtils.kt @@ -0,0 +1,9 @@ +package org.openfolder.kotlinasyncapi.springweb.context.annotation.processor + +import com.fasterxml.jackson.module.kotlin.registerKotlinModule +import io.swagger.v3.core.converter.ModelConverters +import io.swagger.v3.core.jackson.ModelResolver + +internal val MODEL_RESOLVER = ModelConverters().also { + (it.converters.first() as ModelResolver).objectMapper().registerKotlinModule() +} diff --git a/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/MessageProcessor.kt b/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/MessageProcessor.kt index 5b945a5..d646dae 100644 --- a/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/MessageProcessor.kt +++ b/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/MessageProcessor.kt @@ -1,6 +1,5 @@ package org.openfolder.kotlinasyncapi.springweb.context.annotation.processor -import io.swagger.v3.core.converter.ModelConverters import org.openfolder.kotlinasyncapi.annotation.channel.Message import org.openfolder.kotlinasyncapi.model.Reference import org.openfolder.kotlinasyncapi.model.component.Components @@ -10,8 +9,7 @@ import kotlin.reflect.KClass @Component internal class MessageProcessor : AnnotationProcessor> { override fun process(annotation: Message, context: KClass<*>): Components { - val converters = ModelConverters() - val jsonSchema = converters.readAll(context.java) + val jsonSchema = MODEL_RESOLVER.readAll(context.java) return Components().apply { messages { diff --git a/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/SchemaProcessor.kt b/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/SchemaProcessor.kt index 7873e2e..763c64e 100644 --- a/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/SchemaProcessor.kt +++ b/kotlin-asyncapi-spring-web/src/main/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/SchemaProcessor.kt @@ -1,6 +1,5 @@ package org.openfolder.kotlinasyncapi.springweb.context.annotation.processor -import io.swagger.v3.core.converter.ModelConverters import org.openfolder.kotlinasyncapi.annotation.Schema import org.openfolder.kotlinasyncapi.model.component.Components import org.springframework.stereotype.Component @@ -9,8 +8,7 @@ import kotlin.reflect.KClass @Component internal class SchemaProcessor : AnnotationProcessor> { override fun process(annotation: Schema, context: KClass<*>): Components { - val converters = ModelConverters() - val jsonSchema = converters.readAll(context.java) + val jsonSchema = MODEL_RESOLVER.readAll(context.java) return Components().apply { schemas { diff --git a/kotlin-asyncapi-spring-web/src/test/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/SchemaProcessorTest.kt b/kotlin-asyncapi-spring-web/src/test/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/SchemaProcessorTest.kt index fe9e264..facc3a8 100644 --- a/kotlin-asyncapi-spring-web/src/test/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/SchemaProcessorTest.kt +++ b/kotlin-asyncapi-spring-web/src/test/kotlin/org/openfolder/kotlinasyncapi/springweb/context/annotation/processor/SchemaProcessorTest.kt @@ -25,7 +25,7 @@ internal class SchemaProcessorTest { val id: Int = 0, val name: String, val isTest: Boolean, - val sub: TestSubSchema + val sub: TestSubSchema? ) data class TestSubSchema( diff --git a/kotlin-asyncapi-spring-web/src/test/resources/annotation/annotation_integration.json b/kotlin-asyncapi-spring-web/src/test/resources/annotation/annotation_integration.json index 3cc0b65..9e17007 100644 --- a/kotlin-asyncapi-spring-web/src/test/resources/annotation/annotation_integration.json +++ b/kotlin-asyncapi-spring-web/src/test/resources/annotation/annotation_integration.json @@ -58,6 +58,7 @@ }, "schemas" : { "TestHeaders" : { + "required" : [ "type" ], "type" : "object", "properties" : { "type" : { @@ -69,6 +70,7 @@ "exampleSetFlag" : false }, "TestMessage" : { + "required" : [ "id", "isTest", "name" ], "type" : "object", "properties" : { "id" : { @@ -82,7 +84,7 @@ "exampleSetFlag" : false, "types" : [ "string" ] }, - "test" : { + "isTest" : { "type" : "boolean", "exampleSetFlag" : false, "types" : [ "boolean" ] diff --git a/kotlin-asyncapi-spring-web/src/test/resources/annotation/message_component.json b/kotlin-asyncapi-spring-web/src/test/resources/annotation/message_component.json index 880c5cf..a9d263e 100644 --- a/kotlin-asyncapi-spring-web/src/test/resources/annotation/message_component.json +++ b/kotlin-asyncapi-spring-web/src/test/resources/annotation/message_component.json @@ -18,6 +18,7 @@ }, "schemas" : { "TestPayload" : { + "required" : [ "id", "isTest", "name" ], "type" : "object", "properties" : { "id" : { @@ -31,7 +32,7 @@ "exampleSetFlag" : false, "types" : [ "string" ] }, - "test" : { + "isTest" : { "type" : "boolean", "exampleSetFlag" : false, "types" : [ "boolean" ] diff --git a/kotlin-asyncapi-spring-web/src/test/resources/annotation/schema_component.json b/kotlin-asyncapi-spring-web/src/test/resources/annotation/schema_component.json index 4145537..de10839 100644 --- a/kotlin-asyncapi-spring-web/src/test/resources/annotation/schema_component.json +++ b/kotlin-asyncapi-spring-web/src/test/resources/annotation/schema_component.json @@ -1,6 +1,7 @@ { "schemas" : { "TestSchema" : { + "required" : [ "id", "isTest", "name" ], "type" : "object", "properties" : { "id" : { @@ -14,19 +15,20 @@ "exampleSetFlag" : false, "types" : [ "string" ] }, - "sub" : { - "$ref" : "#/components/schemas/TestSubSchema", - "exampleSetFlag" : false - }, - "test" : { + "isTest" : { "type" : "boolean", "exampleSetFlag" : false, "types" : [ "boolean" ] + }, + "sub" : { + "$ref" : "#/components/schemas/TestSubSchema", + "exampleSetFlag" : false } }, "exampleSetFlag" : false }, "TestSubSchema" : { + "required" : [ "exists" ], "type" : "object", "properties" : { "exists" : { diff --git a/pom.xml b/pom.xml index de49df0..de75804 100644 --- a/pom.xml +++ b/pom.xml @@ -245,6 +245,11 @@ jackson-databind 2.14.2 + + com.fasterxml.jackson.module + jackson-module-kotlin + 2.14.2 + io.swagger.core.v3 swagger-core