Skip to content

Commit

Permalink
#88 better fix: teach jackson to serialize enums using toString(), wh…
Browse files Browse the repository at this point in the history
…ich will respect Swagger's ParameterIn and other enums toString() implementation (which is lower-case)
  • Loading branch information
angryziber committed Dec 13, 2024
1 parent f59f0c2 commit c366905
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* jdbc: @NoTransaction can now be used on jobs
* jdbc: fixed usage of multiple different DataSources when there is an active transaction
* json: TSGenerator will now use more type-safe string template types for java.time classes, e.g. `${number}-${number}-${number}` instead of `string`
* openapi: fix compliancy: output parameter types as lowercase in OpenAPI spec
* jackson: serialize enums using their toString() method by default, this fixes `openapi` module usage with `jackson`
* server: `Server(InetSocketAddress(0))` can now be used to bind to any available port.

# 1.6.9
Expand Down
8 changes: 8 additions & 0 deletions jackson/src/JsonBody.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package klite.jackson

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParseException
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.*
Expand Down Expand Up @@ -29,6 +30,7 @@ fun kliteJsonMapper(kotlinModule: KotlinModule = kotlinModule(), javaTimeModule:
serializationInclusion(JsonInclude.Include.NON_NULL)
addModule(SimpleModule().apply {
addDeserializer(String::class.java, EmptyStringToNullDeserializer)
addSerializer(Enum::class.java, EnumToStringSerializer)
addConverterDeserializers()
})
withCoercionConfigDefaults {
Expand All @@ -43,6 +45,12 @@ object EmptyStringToNullDeserializer: JsonDeserializer<String?>() {
jsonParser.valueAsString?.trimToNull()
}

object EnumToStringSerializer: JsonSerializer<Enum<*>>() {
override fun serialize(value: Enum<*>, gen: JsonGenerator, serializers: SerializerProvider) {
gen.writeString(value.toString())
}
}

fun SimpleModule.addConverterDeserializers() = Converter.forEach { type, converter ->
@Suppress("UNCHECKED_CAST")
addDeserializer(type.java as Class<Any>, object: JsonDeserializer<Any>() {
Expand Down
3 changes: 3 additions & 0 deletions openapi/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
dependencies {
implementation(project(":server"))
api("io.swagger.core.v3:swagger-annotations:2.2.22")

testImplementation(project(":json"))
testImplementation(project(":jackson"))
}
2 changes: 1 addition & 1 deletion openapi/src/OpenAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal fun toOperation(route: Route): Pair<String, Any> {
fun toParameter(p: Param, op: Operation? = null) = mapOf(
"name" to p.name,
"required" to (!p.p.isOptional && !p.p.type.isMarkedNullable),
"in" to toParameterIn(p.source)?.name?.lowercase(),
"in" to toParameterIn(p.source),
"schema" to p.p.type.toJsonSchema(),
) + ((p.p.findAnnotation<Parameter>() ?: op?.parameters?.find { it.name == p.name })?.toNonEmptyValues() ?: emptyMap())

Expand Down
18 changes: 18 additions & 0 deletions openapi/test/JsonSerializationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package klite.openapi

import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import io.swagger.v3.oas.annotations.enums.ParameterIn.PATH
import klite.jackson.kliteJsonMapper
import klite.jackson.render
import org.junit.jupiter.api.Test

class JsonSerializationTest {
@Test fun `ParameterIn serialization using klite-json`() {
expect(klite.json.JsonMapper().render(PATH)).toEqual("\"path\"")
}

@Test fun `ParameterIn serialization using jackson`() {
expect(kliteJsonMapper().render(PATH)).toEqual("\"path\"")
}
}

0 comments on commit c366905

Please sign in to comment.