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

Problem with JsonInclude config overrides for java.util.Map #2573

Closed
SukruthKS opened this issue Dec 18, 2019 · 2 comments
Closed

Problem with JsonInclude config overrides for java.util.Map #2573

SukruthKS opened this issue Dec 18, 2019 · 2 comments
Milestone

Comments

@SukruthKS
Copy link

I'm on version 2.10.1 (latest) and below are the sample code, output and issues with it.

Code:

public class Test
{
  public static class Car
  {
      public String model;
      public Map<String, Integer> properties;
  }

  public static void main( String[] args )
  {
      Map<String, Integer> carProperties = new HashMap<>();
      carProperties.put("Speed", 100);
      carProperties.put("Weight", null);

      Car car = new Car();
      car.model = "F60";
      car.properties = carProperties;

      ObjectMapper mapper1 = new ObjectMapper();
      mapper1.setSerializationInclusion(JsonInclude.Include.NON_NULL);
      System.out.println("Test1: Include.NON_NULL on ObjectMapper");
      System.out.println("CarProperties -> " + mapper1.writeValueAsString(carProperties));
      System.out.println("Car           -> " + mapper1.writeValueAsString(car));
      System.out.println();

      ObjectMapper mapper2 = new ObjectMapper();
      mapper2.setSerializationInclusion(JsonInclude.Include.NON_NULL);
      mapper2.configOverride(Map.class).setInclude(JsonInclude.Value.construct(JsonInclude.Include.USE_DEFAULTS, JsonInclude.Include.USE_DEFAULTS));
      System.out.println("Test2: Include.NON_NULL on ObjectMapper and Include.USE_DEFAULTS override on Map class ");
      System.out.println("CarProperties -> " + mapper2.writeValueAsString(carProperties));
      System.out.println("Car           -> " + mapper2.writeValueAsString(car));
      System.out.println();

      ObjectMapper mapper3 = new ObjectMapper();
      mapper3.setSerializationInclusion(JsonInclude.Include.NON_NULL);
      mapper3.configOverride(Map.class).setInclude(JsonInclude.Value.construct(JsonInclude.Include.ALWAYS, JsonInclude.Include.ALWAYS));
      System.out.println("Test3: Include.NON_NULL on ObjectMapper and Include.ALWAYS override on Map class ");
      System.out.println("CarProperties -> " + mapper3.writeValueAsString(carProperties));
      System.out.println("Car           -> " + mapper3.writeValueAsString(car));
  }
}

Output:

Test1: Include.NON_NULL on ObjectMapper
CarProperties -> {"Speed":100}
Car           -> {"model":"F60","properties":{"Speed":100}}

Test2: Include.NON_NULL on ObjectMapper and Include.USE_DEFAULTS override on Map class 
CarProperties -> {"Speed":100,"Weight":null}
Car           -> {"model":"F60","properties":{"Speed":100}}

Test3: Include.NON_NULL on ObjectMapper and Include.ALWAYS override on Map class 
CarProperties -> {"Speed":100,"Weight":null}
Car           -> {"model":"F60","properties":{"Speed":100}}

Issues:

  • In Test2, Weight property is included when properties map is serialized directly. Config override for Map class is set to USE_DEFAULTS which ultimately should resolve to NON_NULL as set on the mapper object, so weight should not be included and the output should be the same as Test1.

  • In Test3, Weight property is missing when Car is serialized. Include for Map class has been overridden with ALWAYS yet Weight property with null value is ignored when serializing the class. However, it is included when the map is serialized directly.

@cowtowncoder
Copy link
Member

Thank you for reporting this -- starting to have a look and I think there is a problem, as per your description.

cowtowncoder added a commit that referenced this issue Dec 25, 2019
@cowtowncoder cowtowncoder added this to the 2.10.2 milestone Dec 26, 2019
@cowtowncoder cowtowncoder changed the title Serialization of map with null values Problem with JsonInclude config overrides for java.util.Map Dec 26, 2019
@cowtowncoder
Copy link
Member

Ok, yes. Per-class config overrides were not correctly applied for Map types. Fixed for 2.10(.2).

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