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

JsonSerialize(Typing.DYNAMIC) in some cases does not redefine MapperFeature.USE_STATIC_TYPING #1515

Open
garkin opened this issue Feb 2, 2017 · 2 comments
Labels
has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue

Comments

@garkin
Copy link

garkin commented Feb 2, 2017

As unit test output shows, annotation overrides mapper feature only for:

  • single field (annotatedValue)

And it's ignored for:

  • annotated collection (annotatedList)
  • annotated class field (dynamicValue )
  • annotated class collection (dynamicList)

This is not expected behavior (for annotated list field, at least), since MapperFeature#USE_STATIC_TYPING says that:

This global default value can be overridden at class, method or field level by using JsonSerialize.typing() annotation property.

Test bellow is written in Kotlin, sorry for that. Was tested with Jackson 2.8.6

package com.testing.jackson

import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import org.junit.Test
import java.util.*
import kotlin.test.assertEquals


abstract class Base { val a = 1 }
class Derived : Base() { val b = 2 }

@JsonSerialize(typing = JsonSerialize.Typing.DYNAMIC)
abstract class BaseDynamic { val a = 1 }
class DerivedDynamic : BaseDynamic() { val b = 2 }

class Mutter {
    val value: Base = Derived()
    @JsonSerialize(typing = JsonSerialize.Typing.DYNAMIC)
    val annotatedValue: Base = Derived()
    val dynamicValue: BaseDynamic = DerivedDynamic()

    val list = ArrayList<Base>().apply { this.add(Derived()) }
    @JsonSerialize(typing = JsonSerialize.Typing.DYNAMIC)
    val annotatedList = ArrayList<Base>().apply { this.add(Derived()) }
    val dynamicList = ArrayList<BaseDynamic>().apply { this.add(DerivedDynamic()) }
}

class jackson_typing_fails {
    @Test fun go() {
        val mapper = ObjectMapper()
                .enable(MapperFeature.USE_STATIC_TYPING)
        //        .enable(SerializationFeature.INDENT_OUTPUT)
        val writer = mapper.writerFor(Mutter::class.java)
        val v = Mutter()
        val s = writer.writeValueAsString(v)
        // println(s)
        val EXPECTED = """{"value":{"a":1},"annotatedValue":{"a":1,"b":2},"dynamicValue":{"a":1,"b":2},"list":[{"a":1}],"annotatedList":[{"a":1,"b":2}],"dynamicList":[{"a":1,"b":2}]}"""
        assertEquals(EXPECTED, s)
    }
}
org.junit.ComparisonFailure: 
Expected :{"value":{"a":1},"annotatedValue":{"a":1,"b":2},"dynamicValue":{"a":1,"b":2},"list":[{"a":1}],"annotatedList":[{"a":1,"b":2}],"dynamicList":[{"a":1,"b":2}]}
Actual   :{"value":{"a":1},"annotatedValue":{"a":1,"b":2},"dynamicValue":{"a":1},"list":[{"a":1}],"annotatedList":[{"a":1}],"dynamicList":[{"a":1}]}
@cowtowncoder cowtowncoder added 2.9 and removed ACTIVE labels Mar 24, 2017
@cowtowncoder
Copy link
Member

It sounds like there may well be a good. I hope to find time to adapt this to Java; at least some aspects seem straight-forward.

cowtowncoder added a commit that referenced this issue Apr 27, 2017
@cowtowncoder
Copy link
Member

Yes, I can reproduce this.

I hope fixing non-List one is relatively easy, we'll see. List-valued property may be trickier, since semantics are bit unclear; it would make sense for setting to apply to contents (since there's no way to apply them separately to value (List) and content(s) (elements)), but we'll see how easy that is to pass.

@cowtowncoder cowtowncoder added 2.10 and removed 2.9 labels Jul 17, 2018
@cowtowncoder cowtowncoder added 2.12 and removed 2.10 labels Apr 12, 2020
@cowtowncoder cowtowncoder added 2.13 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue and removed 2.12 labels Oct 27, 2020
@cowtowncoder cowtowncoder added 2.14 and removed 2.13 labels Jul 15, 2021
@cowtowncoder cowtowncoder removed the 2.14 label Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Projects
None yet
Development

No branches or pull requests

2 participants