jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Mixin not working on Collections

Open nickstolwijk opened this issue 11 months ago • 6 comments

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

nickstolwijk avatar Mar 11 '24 14:03 nickstolwijk

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)

cowtowncoder avatar Mar 12 '24 02:03 cowtowncoder

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.

cowtowncoder avatar Mar 12 '24 02:03 cowtowncoder

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)

I didn't try that. In the original case I can't change the entities.

nickstolwijk avatar Mar 12 '24 07:03 nickstolwijk

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.

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?

nickstolwijk avatar Mar 12 '24 07:03 nickstolwijk

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.

cowtowncoder avatar Mar 12 '24 16:03 cowtowncoder

Do you have an idea how to dynamically not serialize specific types?

nickstolwijk avatar Mar 12 '24 16:03 nickstolwijk