Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization of is properties #427

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.PropertyName
import com.fasterxml.jackson.databind.cfg.MapperConfig
import com.fasterxml.jackson.databind.introspect.*
import com.fasterxml.jackson.databind.introspect.Annotated
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor
import com.fasterxml.jackson.databind.introspect.AnnotatedField
import com.fasterxml.jackson.databind.introspect.AnnotatedMember
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector
import com.fasterxml.jackson.databind.util.BeanUtil
import java.lang.reflect.Constructor
import java.lang.reflect.Method
Expand Down Expand Up @@ -32,6 +38,10 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c
member.parameterCount == 0) {
return member.name.substringAfter("is").decapitalize().substringBefore('-')
}
} else if (member is AnnotatedMethod && member.declaringClass.isKotlinClass()) {
if (cache.isKotlinGeneratedMethod(member) { it.declaringClass.declaredFields.any { f -> f.name == member.name } }) {
return member.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another possibility would be to implement com.fasterxml.jackson.databind.introspect.AccessorNamingStrategy (or maybe extend DefaultAccessorNamingStrategy impl) and define findNameForXxx().
But not sure if that would necessarily be any simpler (it could be more efficient as names of all fields, methods could be done just once when creating instance but that's probably not a big deal).

}
} else if (member is AnnotatedParameter) {
return findKotlinParameterName(member)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.MapperFeature.SORT_PROPERTIES_ALPHABETICALLY
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.junit.Ignore
import org.junit.Test
import kotlin.test.assertEquals

Expand All @@ -18,7 +17,6 @@ class TestGitHub337 {
private val writer = mapper.writerWithDefaultPrettyPrinter()

@Test
@Ignore
fun test_ClassWithIsFields() {
data class ClassWithIsFields(
val isBooleanField: Boolean,
Expand All @@ -35,7 +33,6 @@ class TestGitHub337 {
}

@Test
@Ignore
fun test_AnnotatedClassWithIsFields() {
data class ClassWithIsFields(
@JsonProperty("isBooleanField") val isBooleanField: Boolean,
Expand Down