Skip to content

Commit 6f86c6a

Browse files
committed
bedre feilmeldinger i serder for Kodeverk og JsonNullable
1 parent f5b92f6 commit 6f86c6a

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

core/src/main/kotlin/no/nav/hjelpemidler/domain/kodeverk/KodeverkJackson.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package no.nav.hjelpemidler.domain.kodeverk
22

3+
import no.nav.hjelpemidler.domain.serialization.error
34
import tools.jackson.core.JsonParser
45
import tools.jackson.databind.BeanProperty
56
import tools.jackson.databind.DeserializationContext
@@ -28,17 +29,18 @@ internal class KodeverkDeserializer : StdScalarDeserializer<Kodeverk<*>> {
2829
}
2930

3031
override fun createContextual(context: DeserializationContext, property: BeanProperty?): ValueDeserializer<*> {
31-
val parentType = property?.type ?: context.contextualType
32-
val enumType = parentType.containedTypeOrUnknown(0)
32+
val parentType = property?.type ?: context.contextualType ?: context.error("Could not determine parent type")
33+
val enumType = parentType.containedType(0) ?: context.error("Could not determine enum type")
3334
val enumDeserializer = context.findContextualValueDeserializer(enumType, property)
3435
if (enumType == this.enumType && enumDeserializer == this.enumDeserializer) return this
3536
return KodeverkDeserializer(this, enumType, enumDeserializer)
3637
}
3738

3839
override fun deserialize(parser: JsonParser, context: DeserializationContext): Kodeverk<*> {
39-
checkNotNull(enumDeserializer)
40+
val deserializer = enumDeserializer
41+
?: context.error("KodeverkDeserializer used without contextualization (enumDeserializer is null)")
4042
return try {
41-
enumDeserializer.deserialize(parser, context) as Kodeverk<*>
43+
deserializer.deserialize(parser, context) as Kodeverk<*>
4244
} catch (e: InvalidFormatException) {
4345
UkjentKode(e.value.toString())
4446
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package no.nav.hjelpemidler.domain.serialization
2+
3+
import tools.jackson.databind.DatabindException
4+
import tools.jackson.databind.DeserializationContext
5+
import tools.jackson.databind.SerializationContext
6+
7+
internal fun SerializationContext.error(message: String): Nothing {
8+
throw DatabindException.from(this, message)
9+
}
10+
11+
internal fun DeserializationContext.error(message: String): Nothing {
12+
throw DatabindException.from(this, message)
13+
}

core/src/main/kotlin/no/nav/hjelpemidler/domain/serialization/JsonNullableJackson.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal class JsonNullableSerializer : StdSerializer<JsonNullable<*>> {
3434

3535
override fun createContextual(context: SerializationContext, property: BeanProperty?): ValueSerializer<*> {
3636
val parentType = property?.type ?: return this
37-
val childType = parentType.containedTypeOrUnknown(0)
37+
val childType = parentType.containedType(0) ?: context.error("Could not determine child type")
3838
val childSerializer = context.findValueSerializer(childType)
3939
if (childType == this.childType && childSerializer == this.childSerializer) return this
4040
return JsonNullableSerializer(this, childType, childSerializer)
@@ -88,16 +88,17 @@ internal class JsonNullableDeserializer : StdDeserializer<JsonNullable<*>> {
8888
}
8989

9090
override fun createContextual(context: DeserializationContext, property: BeanProperty?): ValueDeserializer<*> {
91-
val parentType = property?.type ?: context.contextualType
92-
val childType = parentType.containedTypeOrUnknown(0)
91+
val parentType = property?.type ?: context.contextualType ?: context.error("Could not determine parent type")
92+
val childType = parentType.containedType(0) ?: context.error("Could not determine child type")
9393
val childDeserializer = context.findContextualValueDeserializer(childType, property)
9494
if (childType == this.childType && childDeserializer == this.childDeserializer) return this
9595
return JsonNullableDeserializer(this, childType, childDeserializer)
9696
}
9797

9898
override fun deserialize(parser: JsonParser, context: DeserializationContext): JsonNullable<*> {
99-
checkNotNull(childDeserializer)
100-
val value = childDeserializer.deserialize(parser, context)
99+
val deserializer = childDeserializer
100+
?: context.error("JsonNullableDeserializer used without contextualization (childDeserializer is null)")
101+
val value = deserializer.deserialize(parser, context)
101102
return JsonNullable.Present(value)
102103
}
103104

gradle/libs.versions.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jsoup = "1.22.1"
3030
junit = "6.0.3"
3131
kafka = "4.2.0"
3232
konfig = "1.6.10.0"
33-
kotest = "6.1.5"
33+
kotest = "6.1.6"
3434
kotest-ktor = "2.0.0"
3535
kotlin = "2.3.10"
3636
kotlin-logging = "8.0.01"
@@ -48,7 +48,7 @@ microprofile = "4.1.1"
4848
mockk = "1.14.9"
4949
mustache = "0.9.14"
5050
netty = "4.2.10.Final"
51-
nocommons = "0.16.0"
51+
nocommons = "0.17.0"
5252
openapi = "7.20.0"
5353
openhtmltopdf = "1.1.37"
5454
oracle = "23.26.1.0.0"
@@ -60,8 +60,8 @@ redisson = "4.3.0"
6060
shadow = "8.1.1"
6161
slf4j = "2.0.17"
6262
spotless = "8.3.0"
63-
swagger = "2.2.44"
64-
swaggerParser = "2.1.38"
63+
swagger = "2.2.45"
64+
swaggerParser = "2.1.39"
6565
tbdLibs = "2026.02.19-20.58-521cdd3c"
6666
testcontainers = "2.0.3"
6767
threeten = "1.8.0"

0 commit comments

Comments
 (0)