immutables / immutables

Annotation processor to create immutable objects and builders. Feels like Guava's immutable collections but for regular value objects. JSON, Jackson, Gson, JAX-RS integrations included
http://immutables.org
Apache License 2.0
3.43k stars 274 forks source link

lazyInitBitmap field gets serialized into JSON #799

Open nickstolwijk opened 6 years ago

nickstolwijk commented 6 years ago

I have a simple interface annotated with Immutable annotations. When I serialize this to JSON, I see the field lazyInitBitmap and the value of the lazy property in my Mongo database. How can I prevent this from happening?

   @Value.Immutable
   @Value.Style(builder = "new") // builder has to have constructor
   @JsonDeserialize(builder = ImmutablePage.Builder.class)
   public interface Page {
       Set<Label> getLabels();
       @Lazy
       default boolean hasResults() {
           return !getLabels().isEmpty();
       }
    }
elucash commented 6 years ago

lazy init bitmap is declared as private transient volatile long lazyInitBitmap;. The problem is definitely in mongo serializer you use, many mongo mappers don't use Jackson or whatever JSON binding, but instead doing custom reflection to get values from private fields and/or getters. You can open a bug report to the mongo library to skip transient fields, which should be a good idea even if unrelated to Java binary serialization. The other way is to use new experimental Annotation Injection functionality to apply arbitrary annotations to generated code (i.e. synthetic fields for lazy bitmap in this case), in this case it would be the way to add something like @JsonIgnore or whatever annotation mongo mapper is using to skip fields.

Also Immutables provide mongo repository mapping functionality: http://immutables.github.io/mongo.html