@@ -9,14 +9,15 @@ import tools.jackson.module.kotlin.readValue
99import tools.jackson.databind.MapperFeature
1010import org.junit.jupiter.api.Assertions.assertEquals
1111import org.junit.jupiter.api.Test
12+ import tools.jackson.databind.DeserializationFeature
1213import kotlin.properties.Delegates
1314import kotlin.test.assertNull
1415import kotlin.test.fail
1516
1617private data class DataClassPerson (val name : String , val age : Int )
1718
1819private 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}
0 commit comments