|
| 1 | +package com.fasterxml.jackson.module.kotlin.test |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 4 | +import com.fasterxml.jackson.module.kotlin.KotlinFeature.StrictNullChecks |
| 5 | +import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException |
| 6 | +import com.fasterxml.jackson.module.kotlin.kotlinModule |
| 7 | +import com.fasterxml.jackson.module.kotlin.readValue |
| 8 | +import org.junit.jupiter.api.Assertions.assertArrayEquals |
| 9 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 10 | +import org.junit.jupiter.api.Disabled |
| 11 | +import org.junit.jupiter.api.Test |
| 12 | +import org.junit.jupiter.api.assertThrows |
| 13 | +import kotlin.test.assertNull |
| 14 | + |
| 15 | +class StrictNullChecksTestOld { |
| 16 | + private val mapper = ObjectMapper().registerModule(kotlinModule { enable(StrictNullChecks) }) |
| 17 | + |
| 18 | + /** collection tests */ |
| 19 | + |
| 20 | + private data class ClassWithListOfNullableInt(val samples: List<Int?>) |
| 21 | + |
| 22 | + @Test |
| 23 | + fun testListOfNullableInt() { |
| 24 | + val json = """{"samples":[1, null]}""" |
| 25 | + val stateObj = mapper.readValue<ClassWithListOfNullableInt>(json) |
| 26 | + assertEquals(listOf(1, null), stateObj.samples) |
| 27 | + } |
| 28 | + |
| 29 | + private data class ClassWithListOfInt(val samples: List<Int>) |
| 30 | + |
| 31 | + @Test |
| 32 | + fun testListOfInt() { |
| 33 | + assertThrows<MissingKotlinParameterException> { |
| 34 | + val json = """{"samples":[1, null]}""" |
| 35 | + mapper.readValue<ClassWithListOfInt>(json) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private data class ClassWithNullableListOfInt(val samples: List<Int>?) |
| 40 | + |
| 41 | + @Test |
| 42 | + fun testNullableListOfInt() { |
| 43 | + val json = """{"samples": null}""" |
| 44 | + val stateObj = mapper.readValue<ClassWithNullableListOfInt>(json) |
| 45 | + assertNull(stateObj.samples) |
| 46 | + } |
| 47 | + |
| 48 | + /** array tests */ |
| 49 | + |
| 50 | + private data class ClassWithArrayOfNullableInt(val samples: Array<Int?>) |
| 51 | + |
| 52 | + @Test |
| 53 | + fun testArrayOfNullableInt() { |
| 54 | + val json = """{"samples":[1, null]}""" |
| 55 | + val stateObj = mapper.readValue<ClassWithArrayOfNullableInt>(json) |
| 56 | + assertArrayEquals(arrayOf(1, null), stateObj.samples) |
| 57 | + } |
| 58 | + |
| 59 | + private data class ClassWithArrayOfInt(val samples: Array<Int>) |
| 60 | + |
| 61 | + @Test |
| 62 | + fun testArrayOfInt() { |
| 63 | + assertThrows<MissingKotlinParameterException> { |
| 64 | + val json = """{"samples":[1, null]}""" |
| 65 | + mapper.readValue<ClassWithArrayOfInt>(json) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private data class ClassWithNullableArrayOfInt(val samples: Array<Int>?) |
| 70 | + |
| 71 | + @Test |
| 72 | + fun testNullableArrayOfInt() { |
| 73 | + val json = """{"samples": null}""" |
| 74 | + val stateObj = mapper.readValue<ClassWithNullableArrayOfInt>(json) |
| 75 | + assertNull(stateObj.samples) |
| 76 | + } |
| 77 | + |
| 78 | + /** map tests */ |
| 79 | + |
| 80 | + private data class ClassWithMapOfStringToNullableInt(val samples: Map<String, Int?>) |
| 81 | + |
| 82 | + @Test |
| 83 | + fun testMapOfStringToNullableInt() { |
| 84 | + val json = """{ "samples": { "key": null } }""" |
| 85 | + val stateObj = mapper.readValue<ClassWithMapOfStringToNullableInt>(json) |
| 86 | + assertEquals(mapOf<String, Int?>("key" to null), stateObj.samples) |
| 87 | + } |
| 88 | + |
| 89 | + private data class ClassWithMapOfStringToInt(val samples: Map<String, Int>) |
| 90 | + |
| 91 | + @Test |
| 92 | + fun testMapOfStringToIntWithNullValue() { |
| 93 | + assertThrows<MissingKotlinParameterException> { |
| 94 | + val json = """{ "samples": { "key": null } }""" |
| 95 | + mapper.readValue<ClassWithMapOfStringToInt>(json) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private data class ClassWithNullableMapOfStringToInt(val samples: Map<String, Int>?) |
| 100 | + |
| 101 | + @Test |
| 102 | + fun testNullableMapOfStringToInt() { |
| 103 | + val json = """{"samples": null}""" |
| 104 | + val stateObj = mapper.readValue<ClassWithNullableMapOfStringToInt>(json) |
| 105 | + assertNull(stateObj.samples) |
| 106 | + } |
| 107 | + |
| 108 | + /** generics test */ |
| 109 | + |
| 110 | + private data class TestClass<T>(val samples: T) |
| 111 | + |
| 112 | + @Test |
| 113 | + fun testListOfGeneric() { |
| 114 | + val json = """{"samples":[1, 2]}""" |
| 115 | + val stateObj = mapper.readValue<TestClass<List<Int>>>(json) |
| 116 | + assertEquals(listOf(1, 2), stateObj.samples) |
| 117 | + } |
| 118 | + |
| 119 | + @Disabled // this is a hard problem to solve and is currently not addressed |
| 120 | + @Test |
| 121 | + fun testListOfGenericWithNullValue() { |
| 122 | + assertThrows<MissingKotlinParameterException> { |
| 123 | + val json = """{"samples":[1, null]}""" |
| 124 | + mapper.readValue<TestClass<List<Int>>>(json) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + fun testMapOfGeneric() { |
| 130 | + val json = """{ "samples": { "key": 1 } }""" |
| 131 | + val stateObj = mapper.readValue<TestClass<Map<String, Int>>>(json) |
| 132 | + assertEquals(mapOf("key" to 1), stateObj.samples) |
| 133 | + } |
| 134 | + |
| 135 | + @Disabled // this is a hard problem to solve and is currently not addressed |
| 136 | + @Test |
| 137 | + fun testMapOfGenericWithNullValue() { |
| 138 | + assertThrows<MissingKotlinParameterException> { |
| 139 | + val json = """{ "samples": { "key": null } }""" |
| 140 | + mapper.readValue<TestClass<Map<String, Int>>>(json) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + fun testArrayOfGeneric() { |
| 146 | + val json = """{"samples":[1, 2]}""" |
| 147 | + val stateObj = mapper.readValue<TestClass<Array<Int>>>(json) |
| 148 | + assertArrayEquals(arrayOf(1, 2), stateObj.samples) |
| 149 | + } |
| 150 | + |
| 151 | + @Disabled // this is a hard problem to solve and is currently not addressed |
| 152 | + @Test |
| 153 | + fun testArrayOfGenericWithNullValue() { |
| 154 | + assertThrows<MissingKotlinParameterException> { |
| 155 | + val json = """{"samples":[1, null]}""" |
| 156 | + mapper.readValue<TestClass<Array<Int>>>(json) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments