Skip to content

not-null requirement ignored for primitives in data classes #242

Closed
@magicfoodhand

Description

@magicfoodhand

When deserializing a data class with primitive types, the default value for that java primitive type is used instead of throwing an IllegalArgumentException or MissingKotlinParameterException.

Steps to reproduce

  1. Create new project using maven archetype - org.jetbrains.kotlin:kotlin-archetype-jvm
  2. Add dependency on jackson-module-kotlin - 2.9.7
  3. Run the following test class
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.Test
import java.lang.IllegalArgumentException
import kotlin.test.assertNull

data class NullIntValue(val value: Int?)
data class NullDoubleValue(val value: Double?)
data class NullLongValue(val value: Long?)
data class NullBooleanValue(val value: Boolean?)

data class IntValue(val value: Int)
data class DoubleValue(val value: Double)
data class LongValue(val value: Long)
data class BooleanValue(val value: Boolean)

class DeserializationTest {
    private val objectMapper = jacksonObjectMapper()

    @Test
    fun `test value is null - Boolean`(){
        val value : NullBooleanValue = objectMapper.readValue("{}")
        assertNull(value.value)
    }

    @Test
    fun `test value is null - Double`(){
        val value : NullDoubleValue = objectMapper.readValue("{}")
        assertNull(value.value)
    }

    @Test
    fun `test value is null - Int`(){
        val value : NullIntValue = objectMapper.readValue("{}")
        assertNull(value.value)
    }

    @Test
    fun `test value is null - Long`(){
        val value : NullLongValue = objectMapper.readValue("{}")
        assertNull(value.value)
    }

    @Test(expected = IllegalArgumentException::class)
    fun `test value throws - Boolean`(){
        val value : BooleanValue = objectMapper.readValue("{}")
        assertNull(value.value)
    }

    @Test(expected = IllegalArgumentException::class)
    fun `test value throws - Double`(){
        val value : DoubleValue = objectMapper.readValue("{}")
        assertNull(value.value)
    }

    @Test(expected = IllegalArgumentException::class)
    fun `test value throws - Int`(){
        val value : IntValue = objectMapper.readValue("{}")
        assertNull(value.value)
    }

    @Test(expected = IllegalArgumentException::class)
    fun `test value throws - Long`(){
        val value : LongValue = objectMapper.readValue("{}")
        assertNull(value.value)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions