jackson-databind
jackson-databind copied to clipboard
Mixin not working on Collections
Search before asking
- [X] I searched in the issues and found nothing similar.
Describe the bug
When I use mixins with @JsonIgnoreType
to skip specific types Jackson does not skip the type when it appears in Collections.
Version Information
No response
Reproduction
I have written a few unit tests to explain the situation. I expect all tests to succeed, but at the moment 2 tests are failing. The code to showcase this is in https://github.com/nickstolwijk/jackson-bug-mixins/
@Test
void objectMapper_should_skip_muppet_in_list() throws JsonProcessingException {
objectMapper.addMixIn(Muppet.class, MuppetMixin.class);
var show = Show.builder()
.muppet(animal)
.build();
final String showJson = objectMapper.writeValueAsString(show);
assertThat(showJson)
.doesNotContain("Animal");
}
@Test
void objectMapper_should_skip_muppet_in_map() throws JsonProcessingException {
objectMapper.addMixIn(Muppet.class, MuppetMixin.class);
var show = Show.builder()
.puppeteer("Frank Oz", animal)
.build();
final String showJson = objectMapper.writeValueAsString(show);
assertThat(showJson)
.doesNotContain("Animal");
}
@JsonIgnoreType
static class MuppetMixin {
}
Expected behavior
No response
Additional context
No response
One clarifying question (just to be sure): would it work if @JsonIgnoreType
was directly on Muppet
? (I assume so due to mention of mix-in)
One thing to note is that @JsonIgnoreType
itself will not work dynamically: it will (and can) only work when type declaration (of POJO properties, or Collection
, Map
and array element type) indicates type.
So it would not (and could not, will not) work if sub-type of Collection elements is not ignorable type.
One clarifying question (just to be sure): would it work if
@JsonIgnoreType
was directly onMuppet
? (I assume so due to mention of mix-in)
I didn't try that. In the original case I can't change the entities.
One thing to note is that
@JsonIgnoreType
itself will not work dynamically: it will (and can) only work when type declaration (of POJO properties, orCollection
,Map
and array element type) indicates type. So it would not (and could not, will not) work if sub-type of Collection elements is not ignorable type.
Then this is not the correct route for the original case. There I have a Map<String, Object> where the to be ignored types are in.
Is there any way to dynamically ignore types?
There is no out-of-the-box way to dynamically ignore by type: feature was originally intended at dropping POJO properties by static type. Ignoral is calculated when construction serializers/deserializers and literally drops otherwise introspected property from (de)serializer.
Do you have an idea how to dynamically not serialize specific types?