Skip to content

Commit

Permalink
Fix #807 by enabling MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS o…
Browse files Browse the repository at this point in the history
…n selected tests (#811)
  • Loading branch information
cowtowncoder authored Jun 23, 2024
1 parent 9622a71 commit 89f251b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tools.jackson.module.kotlin.kogeraIntegration.deser.valueClass

import com.fasterxml.jackson.annotation.JacksonInject
import tools.jackson.module.kotlin.jacksonObjectMapper
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Test
import tools.jackson.databind.InjectableValues
import tools.jackson.databind.MapperFeature
import tools.jackson.module.kotlin.jacksonMapperBuilder
import tools.jackson.module.kotlin.jacksonObjectMapper

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

val reader = jacksonObjectMapper()
val reader = jacksonMapperBuilder()
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.build()
.readerFor(DataBind4218FailingDto::class.java)
.with(injectables)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import tools.jackson.module.kotlin.readValue
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import tools.jackson.databind.MapperFeature
import kotlin.properties.Delegates
import kotlin.test.assertNull
import kotlin.test.fail
Expand All @@ -19,6 +20,9 @@ private data class DataClassPerson(val name: String, val age: Int)

class TestM11Changes {
val mapper = jacksonObjectMapper()
val mapperWithFinalFieldsAsMutators = jacksonMapperBuilder()
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.build()

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

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

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

assertThat(actualJson, equalTo(expectedJson))
assertThat(newPerson, equalTo(expectedPerson))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import tools.jackson.module.kotlin.*
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import tools.jackson.databind.MapperFeature
import java.io.StringWriter
import java.util.*
import kotlin.properties.Delegates
Expand Down Expand Up @@ -48,6 +49,7 @@ class ParameterNameTests {
private val pascalCasedJson = """{"Name":"Frank","Age":30,"PrimaryAddress":"something here","Renamed":true,"CreatedDt":"2016-10-25T18:25:48.000Z","IsName":false}"""

private val normalCasedMapper = jacksonMapperBuilder()
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
import tools.jackson.module.kotlin.jacksonObjectMapper
import tools.jackson.module.kotlin.jacksonMapperBuilder
import org.junit.Test
import tools.jackson.databind.MapperFeature

@Suppress("UNUSED_VARIABLE")
class TestGithub149 {
Expand Down Expand Up @@ -36,6 +37,8 @@ class TestGithub149 {
@Test
fun testDeserializationOfManagedReferences() {
val mapper = jacksonMapperBuilder()
// 22-Jun-2024, tatu: Requires forcibly setting val (final field) hence:
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.changeDefaultVisibility { v -> v
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package tools.jackson.module.kotlin.test.github

import java.util.UUID

import kotlin.test.assertEquals

import org.junit.Test

import com.fasterxml.jackson.annotation.JsonIdentityInfo
import com.fasterxml.jackson.annotation.ObjectIdGenerators
import tools.jackson.databind.MapperFeature
import tools.jackson.module.kotlin.jacksonMapperBuilder
import tools.jackson.module.kotlin.jacksonObjectMapper
import org.junit.Test
import java.util.UUID
import kotlin.test.assertEquals

class TestGithub194 {
val mapperWithFinalFieldsAsMutators = jacksonMapperBuilder()
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.build()

val id: UUID = UUID.fromString("149800a6-7855-4e09-9185-02e442da8013")
val json = """{"id": "$id", "name": "Foo"}"""

@Test
fun testIdentityInfo() {
val mapper = jacksonObjectMapper()
val value = mapper.readValue(json, WithIdentity::class.java)
val value = mapperWithFinalFieldsAsMutators.readValue(json, WithIdentity::class.java)
assertEquals(id, value.id)
assertEquals(id.toString(), value.idString)
assertEquals("Foo", value.name)
Expand All @@ -32,7 +41,7 @@ class TestGithub194 {
@Test
fun testIdentityInfo_WithDefaultId() {
val mapper = jacksonObjectMapper()
val value = mapper.readValue(json, WithIdentityAndDefaultId::class.java)
val value = mapperWithFinalFieldsAsMutators.readValue(json, WithIdentityAndDefaultId::class.java)
assertEquals(id, value.id)
assertEquals(id.toString(), value.idString)
assertEquals("Foo", value.name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package tools.jackson.module.kotlin.test.github

import kotlin.test.assertEquals

import org.junit.Test

import com.fasterxml.jackson.annotation.JsonMerge

import tools.jackson.databind.JsonNode
import tools.jackson.databind.MapperFeature
import tools.jackson.databind.ObjectMapper
import tools.jackson.databind.node.JsonNodeFactory
import tools.jackson.databind.node.ObjectNode
import tools.jackson.module.kotlin.jacksonObjectMapper
import org.junit.Test
import kotlin.test.assertEquals

import tools.jackson.module.kotlin.jacksonMapperBuilder

class TestGithub211 {
val mapperWithFinalFieldsAsMutators = jacksonMapperBuilder()
.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.build()

@Test
fun simple() {
val original = Person("original", Address("Rivoli", "Paris"))
val changes = JsonNodeFactory.instance.objectNode().put("username", "updated")

val merged = JsonMerger(jacksonObjectMapper()).merge(original, changes)
val merged = JsonMerger(mapperWithFinalFieldsAsMutators).merge(original, changes)

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

val merged = JsonMerger(jacksonObjectMapper()).merge(original, nestedChanges())
val merged = JsonMerger(mapperWithFinalFieldsAsMutators).merge(original, nestedChanges())

assertEquals(Person("updated", Address("Magenta", "Paris")), merged)
}
Expand Down

0 comments on commit 89f251b

Please sign in to comment.