Skip to content

JsonBackReference is being set too late; breaks hashCode() #390

@zAlbee

Description

@zAlbee

I've mapped a parent-children relationship in Jackson like so:

class Parent {
    @JsonManagedReference
    Set<Child> children; // defaults to HashSet
}

class Child {
    @JsonBackReference
    Parent parent;
}

and I am expecting the Jackson deserializer to properly set the value of Child.parent automatically.

I'm seeing an issue after deserialization of a parent (i.e. JSON to POJO), where set membership tests (children.contains(...)) don't work. This is a HashSet and the Child.hashCode() is implemented such that it uses all properties (including the value of Child.parent). I see that Child.parent is getting set for each child, but only AFTER hashCode() has been called already. Stepping through with a debugger the parent property is null at this point when Jackson calls set.add():

    Child.hashCode() line: 133  
    HashMap<K,V>.hash(Object) line: 351 
    HashMap<K,V>.put(K, V) line: 471    
    HashSet<E>.add(E) line: 217 
    CollectionDeserializer.deserialize(JsonParser, DeserializationContext, Collection<Object>) line: 230    
    CollectionDeserializer.deserialize(JsonParser, DeserializationContext) line: 203    
    CollectionDeserializer.deserialize(JsonParser, DeserializationContext) line: 23 
    MethodProperty(SettableBeanProperty).deserialize(JsonParser, DeserializationContext) line: 375  
    ManagedReferenceProperty.deserializeAndSet(JsonParser, DeserializationContext, Object) line: 101    
    BeanDeserializer.deserializeFromObject(JsonParser, DeserializationContext) line: 308    
    BeanDeserializer.deserialize(JsonParser, DeserializationContext) line: 121  

This is with Jackson 2.1.4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions