owlike/genson

NPE in CircularClassReferenceConverterFactory on multi-threaded Genson use

Closed this issue · 1 comments

When trying to serialize messages with recursive attribute types Genson throw NPE in CircularClassReferenceConverterFactory:

Caused by: com.owlike.genson.JsonBindingException: Could not serialize property 'messageType' from class com.sample.Message
	at com.owlike.genson.reflect.PropertyAccessor.couldNotSerialize(PropertyAccessor.java:47) ~[genson-1.4.jar:na]
	at com.owlike.genson.reflect.PropertyAccessor.serialize(PropertyAccessor.java:29) ~[genson-1.4.jar:na]
	at com.owlike.genson.reflect.BeanDescriptor.serialize(BeanDescriptor.java:92) ~[genson-1.4.jar:na]
	at com.owlike.genson.convert.ClassMetadataConverter.serialize(ClassMetadataConverter.java:83) ~[genson-1.4.jar:na]
	at com.owlike.genson.convert.RuntimeTypeConverter.serialize(RuntimeTypeConverter.java:43) ~[genson-1.4.jar:na]
	at com.owlike.genson.convert.NullConverterFactory$NullConverterWrapper.serialize(NullConverterFactory.java:69) ~[genson-1.4.jar:na]
	at com.owlike.genson.Genson.serialize(Genson.java:274) ~[genson-1.4.jar:na]
	... 114 common frames omitted
Caused by: com.owlike.genson.JsonBindingException: Could not serialize property 'parent' from class com.sample.TypeInfo
	at com.owlike.genson.reflect.PropertyAccessor.couldNotSerialize(PropertyAccessor.java:47) ~[genson-1.4.jar:na]
	at com.owlike.genson.reflect.PropertyAccessor.serialize(PropertyAccessor.java:29) ~[genson-1.4.jar:na]
	at com.owlike.genson.reflect.BeanDescriptor.serialize(BeanDescriptor.java:92) ~[genson-1.4.jar:na]
	at com.owlike.genson.convert.ClassMetadataConverter.serialize(ClassMetadataConverter.java:83) ~[genson-1.4.jar:na]
	at com.owlike.genson.convert.RuntimeTypeConverter.serialize(RuntimeTypeConverter.java:43) ~[genson-1.4.jar:na]
	at com.owlike.genson.convert.NullConverterFactory$NullConverterWrapper.serialize(NullConverterFactory.java:69) ~[genson-1.4.jar:na]
	at com.owlike.genson.reflect.PropertyAccessor.serialize(PropertyAccessor.java:27) ~[genson-1.4.jar:na]
	... 119 common frames omitted
Caused by: java.lang.NullPointerException: null
	at com.owlike.genson.convert.CircularClassReferenceConverterFactory$CircularConverter.serialize(CircularClassReferenceConverterFactory.java:36) ~[genson-1.4.jar:na]
	at com.owlike.genson.reflect.PropertyAccessor.serialize(PropertyAccessor.java:27) ~[genson-1.4.jar:na]
	... 124 common frames omitted

Used data structure is:

public class Message {
      private TypeInfo messageType;
}

public class TypeInfo {
      private TypeInfo parent;
}

This issue is cause by CircularConverter.serialize being called by another thread before CircularConverter.setDelegateConverter populates 'wrapped' attribute:

05:41:08.170 [New I/O worker #6] DEBUG c.o.g.convert.CircularClassReferenceConverterFactory - >>>>>>> CircularConverter::new called, identityHash=87047425
...
05:41:08.171 [New I/O worker #5] DEBUG c.o.g.convert.CircularClassReferenceConverterFactory - >>>>>>> CircularConverter::serialize[wrapper==null] called, identityHash=87047425
05:41:08.171 [New I/O worker #6] DEBUG c.o.g.convert.CircularClassReferenceConverterFactory - >>>>>>> CircularConverter::setDelegateConverter called, identityHash=87047425

Thanks for catching and reporting this one!