-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Unknown
-
None
-
Affects Version/s: 4.0.0
-
Component/s: Codecs
-
(copied to CRM)
public class InternalStateEntity<I> { private class B { public B() { } } private List<B> b = new ArrayList<>(); protected InternalStateEntity() { b.add(new B()); } public List<B> getB() { return b; } } public class MongoStateEntity<K> extends InternalStateEntity<ObjectId> { public MongoStateEntity() { } }
Prior to JAVA-3408 the above code was able to be encoded successfully. Now in 4.0 trying to write the same MongoStateEntity fails with:
Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when encoding using the AutomaticPojoCodec. Encoding a MongoStateEntity: 'pojo.MongoStateEntity@37883b97' failed with the following exception: MongoStateEntity contains generic types that have not been specialised. Top level classes with generic types are not supported by the PojoCodec. A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type. at org.bson.codecs.pojo.AutomaticPojoCodec.encode(AutomaticPojoCodec.java:53) at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63) at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29) at com.mongodb.internal.connection.SplittablePayload$WriteRequestEncoder.encode(SplittablePayload.java:200) at com.mongodb.internal.connection.SplittablePayload$WriteRequestEncoder.encode(SplittablePayload.java:187) at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63) at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29) at com.mongodb.internal.connection.BsonWriterHelper.writeDocument(BsonWriterHelper.java:77) at com.mongodb.internal.connection.BsonWriterHelper.writePayload(BsonWriterHelper.java:59) at com.mongodb.internal.connection.CommandMessage.encodeMessageBodyWithMetadata(CommandMessage.java:145) ...
Prior to JAVA-3408 the typeParameterMap.hasTypeParameters() check would return false for MongoStateEntity. Now it returns true which prevents encoding/decoding this class.
I believe theĀ toString() representation shows why the check now fails.
Prior to 4.0.0:
TypeParameterMap{fieldToClassParamIndexMap={}}
Once the changes were made in JAVA-3408 the map is now represented as:
TypeParameterMap{fieldToClassParamIndexMap={0=Either{left=null, right=TypeParameterMap{fieldToClassParamIndexMap={}}}}}