Skip to content

Commit 04731af

Browse files
authored
fixes #585 there is no way to support this kind of feature right now (#601)
Cleaned up tests for null to default feature as they can now clearly show what is supported and what is not. There is no way to provide default value for primitive fields which are not set as nullable, because the primitives are initialized to their defaults (0, 0.0, false etc.) and there is no possibility to distinguish if the value was initialized as a default primitive value or it was provided by user in content (in the first case we can use default parameter value but in the latter no)
1 parent 3160cbb commit 04731af

File tree

1 file changed

+142
-60
lines changed

1 file changed

+142
-60
lines changed

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/NullToDefaultTests.kt

Lines changed: 142 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,156 @@ import org.junit.Test
1010

1111
class TestNullToDefault {
1212

13-
private fun createMapper(allowDefaultingByNull: Boolean) = ObjectMapper()
14-
.registerModule(kotlinModule { configure(NullIsSameAsDefault, allowDefaultingByNull) })
13+
private fun createMapper(allowDefaultingByNull: Boolean) = ObjectMapper()
14+
.registerModule(kotlinModule { configure(NullIsSameAsDefault, allowDefaultingByNull) })
1515

16-
private data class TestClass(val sku: Int = -1,
17-
val text: String,
18-
val name: String = "",
19-
val images: String?,
20-
val language: String = "uk",
21-
val attribute: Int = 0,
22-
val order: Int = -1)
16+
private data class TestClass(
17+
val sku: Int? = -1,
18+
val text: String,
19+
val images: String? = "some image",
20+
val language: String = "uk",
21+
val temperature: Double? = 24.7,
22+
val canBeProcessed: Boolean? = true,
23+
)
2324

24-
@Test
25-
fun shouldUseNullAsDefault() {
26-
val item = createMapper(true).readValue<TestClass>(
27-
"""{
28-
"sku": "974",
25+
data class TestClassWithNotNullPrimitives(
26+
val sku: Int = -1,
27+
val text: String,
28+
val temperature: Double = 24.7,
29+
val canBeProcessed: Boolean = true,
30+
)
31+
32+
@Test
33+
fun shouldUseNullAsDefault() {
34+
val item = createMapper(true).readValue<TestClass>(
35+
"""{
36+
"sku": null,
2937
"text": "plain",
30-
"name": null,
3138
"images": null,
32-
"attribute": "19"
33-
}""")
34-
35-
Assert.assertTrue(item.sku == 974)
36-
Assert.assertTrue(item.text == "plain")
37-
@Suppress("SENSELESS_COMPARISON")
38-
Assert.assertTrue(item.name != null)
39-
Assert.assertTrue(item.images == null)
40-
Assert.assertTrue(item.language == "uk")
41-
Assert.assertTrue(item.attribute == 19)
42-
Assert.assertTrue(item.order == -1)
43-
}
39+
"language": null,
40+
"temperature": null,
41+
"canBeProcessed": null
42+
}"""
43+
)
44+
45+
Assert.assertEquals(-1, item.sku)
46+
Assert.assertEquals("plain", item.text)
47+
Assert.assertEquals("some image", item.images)
48+
Assert.assertEquals("uk", item.language)
49+
Assert.assertTrue(item.temperature == 24.7)
50+
Assert.assertEquals(true, item.canBeProcessed)
51+
}
4452

45-
@Test(expected = MissingKotlinParameterException::class)
46-
fun shouldNotUseNullAsDefault() {
47-
val item = createMapper(false).readValue<TestClass>(
48-
"""{
53+
@Test
54+
fun shouldUseRealValuesInsteadOfDefaultsWhenProvided() {
55+
val item = createMapper(true).readValue<TestClass>(
56+
"""{
57+
"sku": "0",
58+
"text": "plain",
59+
"images": "image1",
60+
"language": "pl",
61+
"temperature": "0.0",
62+
"canBeProcessed": "false"
63+
}"""
64+
)
65+
66+
Assert.assertEquals(0, item.sku)
67+
Assert.assertEquals("plain", item.text)
68+
Assert.assertEquals("image1", item.images)
69+
Assert.assertEquals("pl", item.language)
70+
Assert.assertTrue(item.temperature == 0.0)
71+
Assert.assertEquals(false, item.canBeProcessed)
72+
}
73+
74+
@Test
75+
fun shouldNotUseNullAsDefault() {
76+
val item = createMapper(false).readValue<TestClass>(
77+
"""{
4978
"sku": "974",
5079
"text": "plain",
51-
"name": null,
5280
"images": null,
53-
"attribute": "19"
54-
}""")
55-
56-
Assert.assertTrue(item.sku == 974)
57-
Assert.assertTrue(item.text == "plain")
58-
@Suppress("SENSELESS_COMPARISON")
59-
Assert.assertTrue(item.name != null)
60-
Assert.assertTrue(item.images == null)
61-
Assert.assertTrue(item.language == "uk")
62-
Assert.assertTrue(item.attribute == 19)
63-
Assert.assertTrue(item.order == -1)
64-
}
81+
"language": "pl",
82+
"temperature": "36.6",
83+
"canBeProcessed": "false"
84+
}"""
85+
)
6586

66-
@Test(expected = MissingKotlinParameterException::class)
67-
fun errorIfNotDefault() {
68-
val item = createMapper(true).readValue<TestClass>(
69-
"""{
70-
"sku": "974",
71-
"text": null,
72-
"attribute": "19",
73-
"name": null
74-
}""")
75-
76-
Assert.assertTrue(item.sku == 974)
77-
Assert.assertTrue(item.language == "uk")
78-
Assert.assertTrue(item.attribute == 19)
79-
@Suppress("SENSELESS_COMPARISON")
80-
Assert.assertTrue(item.name != null)
81-
Assert.assertTrue(item.order == -1)
87+
Assert.assertEquals(974, item.sku)
88+
Assert.assertEquals("plain", item.text)
89+
Assert.assertEquals(null, item.images)
90+
Assert.assertEquals("pl", item.language)
91+
Assert.assertTrue(item.temperature == 36.6)
92+
Assert.assertEquals(false, item.canBeProcessed)
93+
}
94+
95+
@Test
96+
fun shouldUseDefaultPrimitiveValuesInsteadOfDefaultsWhenProvidingNullForNotNullPrimitives() {
97+
val item = createMapper(false).readValue<TestClassWithNotNullPrimitives>(
98+
"""{
99+
"sku": null,
100+
"text": "plain",
101+
"temperature": null,
102+
"canBeProcessed": null
103+
}"""
104+
)
105+
106+
Assert.assertEquals(0, item.sku)
107+
Assert.assertEquals("plain", item.text)
108+
Assert.assertTrue(item.temperature == 0.0)
109+
Assert.assertEquals(false, item.canBeProcessed)
82110
}
111+
112+
@Test
113+
fun shouldUseDefaultsWhenNotProvidingAnyValueForNotNullPrimitives() {
114+
val item = createMapper(false).readValue<TestClassWithNotNullPrimitives>(
115+
"""{
116+
"text": "plain"
117+
}"""
118+
)
119+
120+
Assert.assertEquals(-1, item.sku)
121+
Assert.assertEquals("plain", item.text)
122+
Assert.assertTrue(item.temperature == 24.7)
123+
Assert.assertEquals(true, item.canBeProcessed)
124+
}
125+
126+
@Test
127+
fun shouldUseDefaultValueWhenItsNotPresentEvenWhenDefaultingByNullIsDisabled() {
128+
val item = createMapper(false).readValue<TestClass>(
129+
"""{
130+
"text": "plain"
131+
}"""
132+
)
133+
134+
Assert.assertEquals(-1, item.sku)
135+
Assert.assertEquals("plain", item.text)
136+
Assert.assertEquals("some image", item.images)
137+
Assert.assertEquals("uk", item.language)
138+
Assert.assertTrue(item.temperature == 24.7)
139+
Assert.assertEquals(true, item.canBeProcessed)
140+
}
141+
142+
@Test(expected = MissingKotlinParameterException::class)
143+
fun shouldThrowExceptionWhenProvidedNullForNotNullFieldWithoutDefault() {
144+
createMapper(true).readValue<TestClass>(
145+
"""{
146+
"text": null
147+
}"""
148+
)
149+
}
150+
151+
@Test
152+
fun shouldUseDefaultValueForNullNestedDataClasses() {
153+
data class InnerDataClass(val someString: String = "someString")
154+
data class OuterDataClass(val innerClass: InnerDataClass = InnerDataClass())
155+
156+
val outerDataClassInstance = createMapper(true).readValue<OuterDataClass>(
157+
"""{
158+
"innerClass": null
159+
}"""
160+
)
161+
162+
val expectedResult = OuterDataClass(InnerDataClass("someString"))
163+
Assert.assertEquals(expectedResult, outerDataClassInstance)
164+
}
83165
}

0 commit comments

Comments
 (0)