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

setSerializationInclusion(JsonInclude.Include.NON_EMPTY doesn't filter empty values from collections #4640

Open
1 task done
Gems opened this issue Jul 24, 2024 · 6 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@Gems
Copy link
Contributor

Gems commented Jul 24, 2024

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Empty Map values in collections don't get filtered out.

Version Information

2.17.2

Reproduction

Here is code for the corresponding unit test:

    final var objectMapper = new ObjectMapper()
        .setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

    final var payload = Map.of(
        "key", "value",
        "empty", Map.of(),
        "list", List.of(
            Map.of("key", "value"),
            Map.of(),
            Map.of(),
            Map.of("key", "value")));

    final var json = objectMapper.writeValueAsString(payload);
    final var deserialized = (Map<?,?>) objectMapper.readValue(json, Map.class);

    final var expected = Map.of(
        "key", "value",
        "list", List.of(
            Map.of("key", "value"),
            Map.of("key", "value")));

    assertThat(deserialized)
        .isEqualTo(expected);

Expected behavior

It's expected that the test would pass.

Additional context

No response

@Gems Gems added the to-evaluate Issue that has been received but not yet evaluated label Jul 24, 2024
@yawkat
Copy link
Member

yawkat commented Jul 24, 2024

I believe JsonInclude only applies to beans and maps.

@cowtowncoder
Copy link
Member

Kind of: It does apply to Collections wrt "simple" empty case, but it does not filter elements. So only ones with Collection.isEmpty() are filtered out, but not ones where there are elements that would be considered empty.

@Gems
Copy link
Contributor Author

Gems commented Jul 25, 2024

Yeah, I noticed it. But I don't think that it's expected behavior that comes to ones mind when then learn about the "non_empty" option.

I pathed the SerializerFactory to make it to produce a CollectionSerializer implented the same way the MapSerializer is implemented. Works well.

I believe it could be a more straightforward behavior having empty collection/array items filtered out in case of the "non_empty" option is provided.

@cowtowncoder
Copy link
Member

Changing this behavior would be major backwards-compatibility problem tho.
And conceptually skipping is only for properties (of POJOs, Maps), not for elements (of Collections, arrays).

So while I understand why it's confusing, I don't think this is something I want to change.

@Gems
Copy link
Contributor Author

Gems commented Jul 25, 2024

No problem. I have my workaround implemented and it solves the problem we have.

Feel free to close the issue.

I'd suggest though leaving it open for comments, so I can share a relevant solution if anyone else has the same issue.

Cheers,
Ed

@cowtowncoder
Copy link
Member

Will leave open for bit, close in future -- closed issued should be googlable still.

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

3 participants