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

ObjectMapper.setSerializationInclusion() is ignored for JsonAnyGetter #2592

Closed
akhomchenko opened this issue Jan 13, 2020 · 1 comment
Closed
Milestone

Comments

@akhomchenko
Copy link
Contributor

First of all: thank you a lot for an awesome library and tremendous work.

Description

I faced an issue while working with log4j2 JSON layout which uses Jackson. log4j2 JSON layout uses JsonAnyGetter to output context map. While .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) is set on the ObjectMapper of log4j2 JSON layout, null and empty values of map are still present in output.

This issue might be reproduced with plain Jackson:

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static class Sample {
        private static final Map<String, String> attributes = new HashMap<>();

        static {
            attributes.put("name", "John");
            attributes.put("empty", "");
            attributes.put("null", null);
        }

        @JsonProperty("map")
        public Map<String, String> getMap() {
            return attributes;
        }

        @JsonAnyGetter
        public Map<String, String> getUnwrappedMap() {
            return attributes;
        }
    }

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

        System.out.println(mapper.writeValueAsString(new Sample()));
    }
}

On versions prior to 2.9.0 (my assumption - because of #1444) output is (I have tested on 2.5.0 and 2.8.11):

{"map":{"null":null,"name":"John","empty":""},"null":null,"name":"John","empty":""}

2.9.0+ (I have tested on both 2.9.0 and 2.10.2):

{"map":{"name":"John"},"null":null,"name":"John","empty":""}

Question

Should this be considered a bug? If it is a bug which version should fix go? Should fix have backward-compatibility flag? Will you accept PR? I can try to prepare one.

Thanks.

P.S. might be related to #2289.

@cowtowncoder
Copy link
Member

Good question! My first instinct is that yes, it sounds like a bug.

Any getters are bit special things and do not follow all the rules, mostly because technically they are not "real" properties, just a bag of key/value pairs serialized like properties. This presents a few practical challenges in making features that are based on properties (declarations in POJOs) work.

But this particular aspect is probably possibly to support at least for overall definitions; and with some work, for any-getter annotations (@JsonInclude) too. What might be more difficult would be config overrides (per-type).
But one fix at a time, so I can first consider global settings.

akhomchenko added a commit to akhomchenko/jackson-databind that referenced this issue Feb 2, 2020
Fix will not work if filter is used as it triggers separate
execution path.
akhomchenko added a commit to akhomchenko/jackson-databind that referenced this issue Feb 2, 2020
Fix will not work if filter is used as it triggers separate
execution path.
@cowtowncoder cowtowncoder added this to the 2.11.0 milestone Feb 5, 2020
@cowtowncoder cowtowncoder changed the title ObjectMapper setSerializationInclusion is ignored for JsonAnyGetter ObjectMapper.setSerializationInclusion() is ignored for JsonAnyGetter Feb 5, 2020
cowtowncoder added a commit that referenced this issue Feb 5, 2020
@cowtowncoder cowtowncoder removed the 2.11 label Apr 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants