Skip to content

Commit

Permalink
Merge pull request #200 from ProjectMapK/fix/738
Browse files Browse the repository at this point in the history
Fixed a problem where the `Nulls` setting was ignored if the input was `undefined`
  • Loading branch information
k163377 committed Dec 21, 2023
2 parents a8bb30d + 883d8ee commit d5d4bd6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ internal class KotlinValueInstantiator(
} else {
when {
paramDef.isOptional || paramDef.isVararg -> return@forEachIndexed
// do not try to create any object if it is nullable and the value is missing
paramDef.isNullable -> null
// to get suitable "missing" value provided by deserializer
else -> valueDeserializer?.getAbsentValue(ctxt)
// to get suitable "missing" value provided by nullValueProvider
else -> jsonProp.nullValueProvider?.getAbsentValue(ctxt)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.github.projectmapk.jackson.module.kogera.zIntegration.deser

import com.fasterxml.jackson.annotation.JsonSetter
import com.fasterxml.jackson.annotation.Nulls
import com.fasterxml.jackson.databind.exc.InvalidNullException
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
import io.github.projectmapk.jackson.module.kogera.readValue
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class JsonSetterTest {
val mapper = jacksonObjectMapper()

data class NullsSkip(@JsonSetter(nulls = Nulls.SKIP) val v: Int = -1)

@Test
fun nullsSkip() {
val d = mapper.readValue<NullsSkip>("""{"v":null}""")

assertEquals(-1, d.v)
}

data class NullsFail(@JsonSetter(nulls = Nulls.FAIL) val v: Int?)

@Nested
inner class NullsFailTest {
@Test
fun onNull() {
assertThrows<InvalidNullException> {
mapper.readValue<NullsFail>("""{"v":null}""")
}
}

@Test
fun onMissing() {
assertThrows<InvalidNullException> {
mapper.readValue<NullsFail>("""{}""")
}
}
}

data class NullsAsEmpty(@JsonSetter(nulls = Nulls.AS_EMPTY) val v: Collection<*>)

@Nested
inner class NullsAsEmptyTest {
@Test
fun onNull() {
val d = mapper.readValue<NullsAsEmpty>("""{"v":null}""")
assertTrue(d.v.isEmpty())
}

@Test
fun onMissing() {
val d = mapper.readValue<NullsAsEmpty>("""{}""")
assertTrue(d.v.isEmpty())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.projectmapk.jackson.module.kogera.zIntegration.deser.deserializer

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
import io.github.projectmapk.jackson.module.kogera.readValue
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class MissingAbsentValueTest {
class Deser : StdDeserializer<Int>(Int::class.java) {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Int {
TODO("Not yet implemented")
}

override fun getAbsentValue(ctxt: DeserializationContext) = -1
}

data class D(
@JsonDeserialize(using = Deser::class) val foo: Int,
@JsonDeserialize(using = Deser::class) val bar: Int?
)

@Test
fun test() {
val mapper = jacksonObjectMapper()
val result = mapper.readValue<D>("{}")

assertEquals(D(-1, -1), result)
}
}

This file was deleted.

0 comments on commit d5d4bd6

Please sign in to comment.