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

Mixin not working on Collections #4424

Open
1 task done
nickstolwijk opened this issue Mar 11, 2024 · 6 comments
Open
1 task done

Mixin not working on Collections #4424

nickstolwijk opened this issue Mar 11, 2024 · 6 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@nickstolwijk
Copy link

nickstolwijk commented Mar 11, 2024

Search before asking

  • 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 nickstolwijk added the to-evaluate Issue that has been received but not yet evaluated label Mar 11, 2024
@cowtowncoder
Copy link
Member

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
Copy link
Member

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.

@nickstolwijk
Copy link
Author

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
Copy link
Author

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?

@cowtowncoder
Copy link
Member

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.

@nickstolwijk
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

2 participants