Skip to content

Commit 348fcbb

Browse files
authored
fix failing tests post databind#4858 (#880)
1 parent 489b749 commit 348fcbb

File tree

5 files changed

+54
-26
lines changed

5 files changed

+54
-26
lines changed

src/test/kotlin/tools/jackson/module/kotlin/test/KotlinFeatures.kt

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import tools.jackson.module.kotlin.readValue
99
import tools.jackson.databind.MapperFeature
1010
import org.junit.jupiter.api.Assertions.assertEquals
1111
import org.junit.jupiter.api.Test
12+
import tools.jackson.databind.DeserializationFeature
1213
import kotlin.properties.Delegates
1314
import kotlin.test.assertNull
1415
import kotlin.test.fail
1516

1617
private data class DataClassPerson(val name: String, val age: Int)
1718

1819
private class TestM11Changes {
19-
val mapper = jacksonObjectMapper()
20+
val MAPPER = jacksonObjectMapper()
2021
val mapperWithFinalFieldsAsMutators = jacksonMapperBuilder()
2122
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
2223
.build()
@@ -27,8 +28,8 @@ private class TestM11Changes {
2728
val expectedJson = """{"name":"John Smith","age":30}"""
2829
val expectedPerson = Class_With_One_Constructor("John Smith", 30)
2930

30-
val actualJson = mapper.writeValueAsString(expectedPerson)
31-
val newPerson = mapper.readValue<Class_With_One_Constructor>(actualJson)
31+
val actualJson = MAPPER.writeValueAsString(expectedPerson)
32+
val newPerson = MAPPER.readValue<Class_With_One_Constructor>(actualJson)
3233

3334
assertEquals(expectedJson, actualJson)
3435
assertEquals(expectedPerson.name, newPerson.name)
@@ -41,8 +42,8 @@ private class TestM11Changes {
4142
val expectedJson = """{"name":"John Smith","age":30}"""
4243
val expectedPerson = Class_Data_Annotation_With_One_Constructor("John Smith", 30)
4344

44-
val actualJson = mapper.writeValueAsString(expectedPerson)
45-
val newPerson = mapper.readValue<Class_Data_Annotation_With_One_Constructor>(actualJson)
45+
val actualJson = MAPPER.writeValueAsString(expectedPerson)
46+
val newPerson = MAPPER.readValue<Class_Data_Annotation_With_One_Constructor>(actualJson)
4647

4748
assertEquals(expectedJson, actualJson)
4849
assertEquals(expectedPerson, newPerson)
@@ -78,8 +79,8 @@ private class TestM11Changes {
7879
val expectedJson = """{"name":"John Smith","age":30}"""
7980
val expectedPerson = Class_With_Init_Constructor_And_Ignored_Property("John Smith", 30)
8081

81-
val actualJson = mapper.writeValueAsString(expectedPerson)
82-
val newPerson = mapper.readValue<Class_With_Init_Constructor_And_Ignored_Property>(actualJson)
82+
val actualJson = MAPPER.writeValueAsString(expectedPerson)
83+
val newPerson = MAPPER.readValue<Class_With_Init_Constructor_And_Ignored_Property>(actualJson)
8384

8485
assertEquals(expectedJson, actualJson)
8586
assertEquals(expectedPerson, newPerson)
@@ -93,8 +94,8 @@ private class TestM11Changes {
9394
val expectedJson = """{"name":"John Smith","age":30}"""
9495
val expectedPerson = Class_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter("John Smith", 30)
9596

96-
val actualJson = mapper.writeValueAsString(expectedPerson)
97-
val newPerson = mapper.readValue<Class_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter>(actualJson)
97+
val actualJson = MAPPER.writeValueAsString(expectedPerson)
98+
val newPerson = MAPPER.readValue<Class_With_No_Field_Parameters_But_Field_Declared_Inside_initialized_from_parameter>(actualJson)
9899

99100
assertEquals(expectedJson, actualJson)
100101
assertEquals(expectedPerson.name, newPerson.name)
@@ -114,8 +115,8 @@ private class TestM11Changes {
114115
val expectedJson = """{"name":"John Smith","age":30}"""
115116
val expectedPerson = ClassFor_testDataClass_WithOnlySecondaryConstructor("John Smith", 30)
116117

117-
val actualJson = mapper.writeValueAsString(expectedPerson)
118-
val newPerson = mapper.readValue<ClassFor_testDataClass_WithOnlySecondaryConstructor>(actualJson)
118+
val actualJson = MAPPER.writeValueAsString(expectedPerson)
119+
val newPerson = MAPPER.readValue<ClassFor_testDataClass_WithOnlySecondaryConstructor>(actualJson)
119120

120121
assertEquals(expectedJson, actualJson)
121122
assertEquals(expectedPerson.name, newPerson.name)
@@ -131,8 +132,8 @@ private class TestM11Changes {
131132
val expectedJson = """{"name":"John Smith","age":30}"""
132133
val expectedPerson = Class_WithPrimaryAndSecondaryConstructor("John Smith", 30)
133134

134-
val actualJson = mapper.writeValueAsString(expectedPerson)
135-
val newPerson = mapper.readValue<Class_WithPrimaryAndSecondaryConstructor>(actualJson)
135+
val actualJson = MAPPER.writeValueAsString(expectedPerson)
136+
val newPerson = MAPPER.readValue<Class_WithPrimaryAndSecondaryConstructor>(actualJson)
136137

137138
assertEquals(expectedJson, actualJson)
138139
assertEquals(expectedPerson.name, newPerson.name)
@@ -145,17 +146,21 @@ private class TestM11Changes {
145146
)
146147

147148
@Test fun testDataClass_WithPrimaryAndSecondaryConstructorBothCouldBeUsedToDeserialize() {
149+
val disabledMapper = jacksonMapperBuilder()
150+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
151+
.build();
152+
148153
val expectedJson = """{"name":"John Smith","age":30}"""
149154
val expectedPerson = Class_WithPrimaryAndSecondaryConstructorAnnotated("John Smith", 30)
150155

151-
val actualJson = mapper.writeValueAsString(expectedPerson)
152-
val newPerson = mapper.readValue<Class_WithPrimaryAndSecondaryConstructorAnnotated>(actualJson)
156+
val actualJson = disabledMapper.writeValueAsString(expectedPerson)
157+
val newPerson = disabledMapper.readValue<Class_WithPrimaryAndSecondaryConstructorAnnotated>(actualJson)
153158

154159
assertEquals(expectedJson, actualJson)
155160
assertEquals(expectedPerson.name, newPerson.name)
156161
assertEquals(expectedPerson.age, newPerson.age)
157162

158-
val newPerson2 = mapper.readValue<Class_WithPrimaryAndSecondaryConstructorAnnotated>("""{"name":"John Smith"}""")
163+
val newPerson2 = disabledMapper.readValue<Class_WithPrimaryAndSecondaryConstructorAnnotated>("""{"name":"John Smith"}""")
159164
assertEquals(0, newPerson2.age)
160165
assertEquals("John Smith", newPerson2.name)
161166
}
@@ -171,8 +176,8 @@ private class TestM11Changes {
171176
val expectedPerson = Class_WithPartialFieldsInConstructor("John Smith", 30)
172177
expectedPerson.phone = "1234567890"
173178

174-
val actualJson = mapper.writeValueAsString(expectedPerson)
175-
val newPerson = mapper.readValue<Class_WithPartialFieldsInConstructor>(actualJson)
179+
val actualJson = MAPPER.writeValueAsString(expectedPerson)
180+
val newPerson = MAPPER.readValue<Class_WithPartialFieldsInConstructor>(actualJson)
176181

177182
assertEquals(expectedJson, actualJson)
178183
assertEquals(expectedPerson.name, newPerson.name)
@@ -181,7 +186,7 @@ private class TestM11Changes {
181186
assertEquals(expectedPerson.primaryAddress, newPerson.primaryAddress)
182187

183188
val jsonWithNullPhone = """{"name":"John Smith","age":30}"""
184-
val person = mapper.readValue<Class_WithPartialFieldsInConstructor>(jsonWithNullPhone)
189+
val person = MAPPER.readValue<Class_WithPartialFieldsInConstructor>(jsonWithNullPhone)
185190

186191
try {
187192
person.phone
@@ -191,7 +196,7 @@ private class TestM11Changes {
191196
}
192197

193198
@Test fun testNullableType() {
194-
val newPerson = mapper.readValue<Class_WithPartialFieldsInConstructor?>("null")
199+
val newPerson = MAPPER.readValue<Class_WithPartialFieldsInConstructor?>("null")
195200
assertNull(newPerson)
196201
}
197202
}

src/test/kotlin/tools/jackson/module/kotlin/test/NullToDefaultTests.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import tools.jackson.module.kotlin.readValue
88
import org.junit.jupiter.api.Assertions.*
99
import org.junit.jupiter.api.Test
1010
import org.junit.jupiter.api.assertThrows
11+
import tools.jackson.databind.DeserializationFeature
1112

1213
class TestNullToDefault {
1314
private fun createMapper(allowDefaultingByNull: Boolean) = JsonMapper.builder()
@@ -100,7 +101,11 @@ class TestNullToDefault {
100101

101102
@Test
102103
fun shouldUseDefaultPrimitiveValuesInsteadOfDefaultsWhenProvidingNullForNotNullPrimitives() {
103-
val item = createMapper(false).readValue<TestClassWithNotNullPrimitives>(
104+
val item = createMapper(false)
105+
.rebuild()
106+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
107+
.build()
108+
.readValue<TestClassWithNotNullPrimitives>(
104109
"""{
105110
"sku": null,
106111
"text": "plain",

src/test/kotlin/tools/jackson/module/kotlin/test/ParameterNameTests.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import tools.jackson.databind.SerializationFeature
88
import tools.jackson.module.kotlin.*
99
import tools.jackson.databind.MapperFeature
1010
import org.junit.jupiter.api.Test
11+
import tools.jackson.databind.DeserializationFeature
1112
import java.io.StringWriter
1213
import java.util.*
1314
import kotlin.properties.Delegates
@@ -192,8 +193,11 @@ class ParameterNameTests {
192193
// data class with non fields appearing as parameters in constructor, this works but null values or defaults for primitive types are passed to
193194
// the unrecognized fields in the constructor. Does not work with default values for parameters, because a null does not get converted to the
194195
// default.
196+
val disabledMapper = normalCasedMapper.rebuild()
197+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
198+
.build();
195199

196-
val stateObj = normalCasedMapper.readValue<StateObjectAsDataClassConfusingConstructor>(normalCasedJson)
200+
val stateObj = disabledMapper.readValue<StateObjectAsDataClassConfusingConstructor>(normalCasedJson)
197201
stateObj.validate()
198202
}
199203

src/test/kotlin/tools/jackson/module/kotlin/test/github/Github26.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@ package tools.jackson.module.kotlin.test.github
33
import tools.jackson.module.kotlin.jacksonObjectMapper
44
import tools.jackson.module.kotlin.readValue
55
import org.junit.jupiter.api.Test
6+
import tools.jackson.databind.DeserializationFeature
7+
import tools.jackson.databind.ObjectMapper
8+
import tools.jackson.module.kotlin.jacksonMapperBuilder
69
import kotlin.test.assertEquals
710

811
data class ClassWithPrimitivesWithDefaults(val i: Int = 5, val x: Int)
912

1013
class TestGithub26 {
1114
@Test fun testConstructorWithPrimitiveTypesDefaultedExplicitlyAndImplicitly() {
12-
val check1: ClassWithPrimitivesWithDefaults = jacksonObjectMapper()
15+
val check1: ClassWithPrimitivesWithDefaults = _createMapper()
1316
.readValue("""{"i":3,"x":2}""")
1417
assertEquals(3, check1.i)
1518
assertEquals(2, check1.x)
1619

17-
val check2: ClassWithPrimitivesWithDefaults = jacksonObjectMapper()
20+
val check2: ClassWithPrimitivesWithDefaults = _createMapper()
1821
.readValue("""{}""")
1922
assertEquals(5, check2.i)
2023
assertEquals(0, check2.x)
2124

22-
val check3: ClassWithPrimitivesWithDefaults = jacksonObjectMapper()
25+
val check3: ClassWithPrimitivesWithDefaults = _createMapper()
2326
.readValue("""{"i": 2}""")
2427
assertEquals(2, check3.i)
2528
assertEquals(0, check3.x)
2629

2730
}
2831

32+
private fun _createMapper(): ObjectMapper {
33+
return jacksonMapperBuilder()
34+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
35+
.build()
36+
}
37+
2938
}

src/test/kotlin/tools/jackson/module/kotlin/test/github/Github27.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import tools.jackson.module.kotlin.test.expectFailure
88
import org.junit.jupiter.api.Assertions
99
import org.junit.jupiter.api.Assertions.assertEquals
1010
import org.junit.jupiter.api.Test
11+
import tools.jackson.databind.DeserializationFeature
1112
import kotlin.test.assertTrue
1213
import kotlin.test.fail
1314

@@ -26,8 +27,12 @@ class TestGithub27 {
2627
private data class ClassWithInt(val sample: Int)
2728

2829
@Test fun testInt() {
30+
val disabledMapper = mapper.rebuild()
31+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
32+
.build();
33+
2934
val json = """{"sample":null}"""
30-
val stateObj = mapper.readValue<ClassWithInt>(json)
35+
val stateObj = disabledMapper.readValue<ClassWithInt>(json)
3136
assertEquals(ClassWithInt(0), stateObj)
3237
}
3338

0 commit comments

Comments
 (0)