Skip to content

Commit 89f251b

Browse files
authored
Fix #807 by enabling MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS on selected tests (#811)
1 parent 9622a71 commit 89f251b

File tree

6 files changed

+46
-14
lines changed

6 files changed

+46
-14
lines changed

src/test/kotlin/tools/jackson/module/kotlin/kogeraIntegration/deser/valueClass/JacksonInjectTest.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass
22

33
import com.fasterxml.jackson.annotation.JacksonInject
4-
import tools.jackson.module.kotlin.jacksonObjectMapper
54
import org.junit.Assert.assertEquals
65
import org.junit.Assert.assertThrows
76
import org.junit.Test
87
import tools.jackson.databind.InjectableValues
8+
import tools.jackson.databind.MapperFeature
9+
import tools.jackson.module.kotlin.jacksonMapperBuilder
10+
import tools.jackson.module.kotlin.jacksonObjectMapper
911

1012
class JacksonInjectTest {
1113
// This is specified as a getter because there is a possibility of problems if it is assigned to a field.
@@ -63,7 +65,9 @@ class JacksonInjectTest {
6365
fun dataBind4218Failing() {
6466
val injectables = InjectableValues.Std(mapOf("pNn" to Primitive(0), "pN" to Primitive(1)))
6567

66-
val reader = jacksonObjectMapper()
68+
val reader = jacksonMapperBuilder()
69+
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
70+
.build()
6771
.readerFor(DataBind4218FailingDto::class.java)
6872
.with(injectables)
6973

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import tools.jackson.module.kotlin.readValue
1010
import org.hamcrest.CoreMatchers.equalTo
1111
import org.hamcrest.MatcherAssert.assertThat
1212
import org.junit.Test
13+
import tools.jackson.databind.MapperFeature
1314
import kotlin.properties.Delegates
1415
import kotlin.test.assertNull
1516
import kotlin.test.fail
@@ -19,6 +20,9 @@ private data class DataClassPerson(val name: String, val age: Int)
1920

2021
class TestM11Changes {
2122
val mapper = jacksonObjectMapper()
23+
val mapperWithFinalFieldsAsMutators = jacksonMapperBuilder()
24+
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
25+
.build()
2226

2327
private class Class_With_One_Constructor(val name: String, val age: Int)
2428

@@ -61,8 +65,9 @@ class TestM11Changes {
6165
val expectedJson = """{"name":"John Smith","age":30,"otherThing":"franky"}"""
6266
val expectedPerson = Class_With_Init_Constructor("John Smith", 30)
6367

64-
val actualJson = mapper.writeValueAsString(expectedPerson)
65-
val newPerson = mapper.readValue<Class_With_Init_Constructor>(actualJson)
68+
// 22-Jun-2024, tatu: Requires forcibly setting val (final field) hence:
69+
val actualJson = mapperWithFinalFieldsAsMutators.writeValueAsString(expectedPerson)
70+
val newPerson = mapperWithFinalFieldsAsMutators.readValue<Class_With_Init_Constructor>(actualJson)
6671

6772
assertThat(actualJson, equalTo(expectedJson))
6873
assertThat(newPerson, equalTo(expectedPerson))

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

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import tools.jackson.module.kotlin.*
99
import org.hamcrest.CoreMatchers.equalTo
1010
import org.hamcrest.MatcherAssert.assertThat
1111
import org.junit.Test
12+
import tools.jackson.databind.MapperFeature
1213
import java.io.StringWriter
1314
import java.util.*
1415
import kotlin.properties.Delegates
@@ -48,6 +49,7 @@ class ParameterNameTests {
4849
private val pascalCasedJson = """{"Name":"Frank","Age":30,"PrimaryAddress":"something here","Renamed":true,"CreatedDt":"2016-10-25T18:25:48.000Z","IsName":false}"""
4950

5051
private val normalCasedMapper = jacksonMapperBuilder()
52+
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
5153
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
5254
.build()
5355

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

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
77
import tools.jackson.module.kotlin.jacksonObjectMapper
88
import tools.jackson.module.kotlin.jacksonMapperBuilder
99
import org.junit.Test
10+
import tools.jackson.databind.MapperFeature
1011

1112
@Suppress("UNUSED_VARIABLE")
1213
class TestGithub149 {
@@ -36,6 +37,8 @@ class TestGithub149 {
3637
@Test
3738
fun testDeserializationOfManagedReferences() {
3839
val mapper = jacksonMapperBuilder()
40+
// 22-Jun-2024, tatu: Requires forcibly setting val (final field) hence:
41+
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
3942
.changeDefaultVisibility { v -> v
4043
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
4144
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
package tools.jackson.module.kotlin.test.github
22

3+
import java.util.UUID
4+
5+
import kotlin.test.assertEquals
6+
7+
import org.junit.Test
8+
39
import com.fasterxml.jackson.annotation.JsonIdentityInfo
410
import com.fasterxml.jackson.annotation.ObjectIdGenerators
11+
import tools.jackson.databind.MapperFeature
12+
import tools.jackson.module.kotlin.jacksonMapperBuilder
513
import tools.jackson.module.kotlin.jacksonObjectMapper
6-
import org.junit.Test
7-
import java.util.UUID
8-
import kotlin.test.assertEquals
914

1015
class TestGithub194 {
16+
val mapperWithFinalFieldsAsMutators = jacksonMapperBuilder()
17+
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
18+
.build()
19+
1120
val id: UUID = UUID.fromString("149800a6-7855-4e09-9185-02e442da8013")
1221
val json = """{"id": "$id", "name": "Foo"}"""
1322

1423
@Test
1524
fun testIdentityInfo() {
1625
val mapper = jacksonObjectMapper()
17-
val value = mapper.readValue(json, WithIdentity::class.java)
26+
val value = mapperWithFinalFieldsAsMutators.readValue(json, WithIdentity::class.java)
1827
assertEquals(id, value.id)
1928
assertEquals(id.toString(), value.idString)
2029
assertEquals("Foo", value.name)
@@ -32,7 +41,7 @@ class TestGithub194 {
3241
@Test
3342
fun testIdentityInfo_WithDefaultId() {
3443
val mapper = jacksonObjectMapper()
35-
val value = mapper.readValue(json, WithIdentityAndDefaultId::class.java)
44+
val value = mapperWithFinalFieldsAsMutators.readValue(json, WithIdentityAndDefaultId::class.java)
3645
assertEquals(id, value.id)
3746
assertEquals(id.toString(), value.idString)
3847
assertEquals("Foo", value.name)

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package tools.jackson.module.kotlin.test.github
22

3+
import kotlin.test.assertEquals
4+
5+
import org.junit.Test
6+
37
import com.fasterxml.jackson.annotation.JsonMerge
8+
49
import tools.jackson.databind.JsonNode
10+
import tools.jackson.databind.MapperFeature
511
import tools.jackson.databind.ObjectMapper
612
import tools.jackson.databind.node.JsonNodeFactory
713
import tools.jackson.databind.node.ObjectNode
8-
import tools.jackson.module.kotlin.jacksonObjectMapper
9-
import org.junit.Test
10-
import kotlin.test.assertEquals
14+
15+
import tools.jackson.module.kotlin.jacksonMapperBuilder
1116

1217
class TestGithub211 {
18+
val mapperWithFinalFieldsAsMutators = jacksonMapperBuilder()
19+
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
20+
.build()
21+
1322
@Test
1423
fun simple() {
1524
val original = Person("original", Address("Rivoli", "Paris"))
1625
val changes = JsonNodeFactory.instance.objectNode().put("username", "updated")
1726

18-
val merged = JsonMerger(jacksonObjectMapper()).merge(original, changes)
27+
val merged = JsonMerger(mapperWithFinalFieldsAsMutators).merge(original, changes)
1928

2029
assertEquals(original.copy(username = "updated"), merged)
2130
}
@@ -24,7 +33,7 @@ class TestGithub211 {
2433
fun nested() {
2534
val original = Person("original", Address("Rivoli", "Paris"))
2635

27-
val merged = JsonMerger(jacksonObjectMapper()).merge(original, nestedChanges())
36+
val merged = JsonMerger(mapperWithFinalFieldsAsMutators).merge(original, nestedChanges())
2837

2938
assertEquals(Person("updated", Address("Magenta", "Paris")), merged)
3039
}

0 commit comments

Comments
 (0)