Skip to content

Commit

Permalink
feat: Add Kotlin module to model resolver (#48)
Browse files Browse the repository at this point in the history
feat - Add Kotlin module to model resolver
  • Loading branch information
lorenzsimon authored Apr 10, 2023
1 parent 3ea409b commit 04d6162
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 14 deletions.
4 changes: 4 additions & 0 deletions kotlin-asyncapi-spring-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,8 +9,7 @@ import kotlin.reflect.KClass
@Component
internal class MessageProcessor : AnnotationProcessor<Message, KClass<*>> {
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,8 +8,7 @@ import kotlin.reflect.KClass
@Component
internal class SchemaProcessor : AnnotationProcessor<Schema, KClass<*>> {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
},
"schemas" : {
"TestHeaders" : {
"required" : [ "type" ],
"type" : "object",
"properties" : {
"type" : {
Expand All @@ -69,6 +70,7 @@
"exampleSetFlag" : false
},
"TestMessage" : {
"required" : [ "id", "isTest", "name" ],
"type" : "object",
"properties" : {
"id" : {
Expand All @@ -82,7 +84,7 @@
"exampleSetFlag" : false,
"types" : [ "string" ]
},
"test" : {
"isTest" : {
"type" : "boolean",
"exampleSetFlag" : false,
"types" : [ "boolean" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"schemas" : {
"TestPayload" : {
"required" : [ "id", "isTest", "name" ],
"type" : "object",
"properties" : {
"id" : {
Expand All @@ -31,7 +32,7 @@
"exampleSetFlag" : false,
"types" : [ "string" ]
},
"test" : {
"isTest" : {
"type" : "boolean",
"exampleSetFlag" : false,
"types" : [ "boolean" ]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"schemas" : {
"TestSchema" : {
"required" : [ "id", "isTest", "name" ],
"type" : "object",
"properties" : {
"id" : {
Expand All @@ -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" : {
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-core</artifactId>
Expand Down

0 comments on commit 04d6162

Please sign in to comment.