[JAVA-2799] Enum is handled as POJO when coexists in the same package as the rest of the POJO classes Created: 05/Mar/18  Updated: 28/Oct/23  Resolved: 07/Mar/18

Status: Closed
Project: Java Driver
Component/s: Codecs
Affects Version/s: 3.6.3
Fix Version/s: 3.6.4, 3.7.0

Type: Bug Priority: Major - P3
Reporter: Igal Rivin (Inactive) Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

When an enum exists in the same package as POJO classes, its type is handled as a POJO and not as an enum.
Consider the package "my_package" and under it the following:

package my_package;
public class MyClass {
 
     public enum MyEnum {
           ENUM_1, ENUM_2;
     }
    
     private MyEnum e;
    
     public MyClass() {
     }
 
     public MyEnum getE() {return e;}
     public void setE(MyEnum e) {this.e = e;}
}
 
// In another package, where MongoClient is created
 
//Register the PojoCodecProvider
PojoCodecProvider pojoCodecProvider = PojoCodecProvider.builder()
                .register("my_package").build();
//.... Rest of the code that associates PojoCodecProvider with MongoClient
 
//When find() is called to bring a MyClass document from the database:
MyClass myClass = myClassCollection.find().first();

The following error occurs:
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Cannot find a public constructor for 'MyEnum'.
	at org.bson.codecs.pojo.CreatorExecutable.checkHasAnExecutable(CreatorExecutable.java:140)
	at org.bson.codecs.pojo.CreatorExecutable.getInstance(CreatorExecutable.java:107)
	at org.bson.codecs.pojo.InstanceCreatorImpl.<init>(InstanceCreatorImpl.java:40)
	at org.bson.codecs.pojo.InstanceCreatorFactoryImpl.create(InstanceCreatorFactoryImpl.java:28)
	at org.bson.codecs.pojo.ClassModel.getInstanceCreator(ClassModel.java:70)
	at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:121)
	at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:126)
	at org.bson.codecs.pojo.LazyPojoCodec.decode(LazyPojoCodec.java:57)
	at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
	at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:184)
	... 67 more

The workaround on this issue might be registering specific classes in PojoCodecProvider rather than a package name, but its not very convenient.
Additionally, creating and registering a custom codec for "MyEnum" enum doesn't help since it seems to be overridden by a PojoCodec.

Thanks,
Igal



 Comments   
Comment by Githook User [ 07/Mar/18 ]

Author:

{'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley', 'username': 'rozza'}

Message: Fix PojoCodecProvider handling of registered packages

Ensure registered packages and automatic codecs work in the
same manner.

JAVA-2799
Branch: 3.6.x
https://github.com/mongodb/mongo-java-driver/commit/b0d0ed4798545b232ae4033179a50937a9642938

Comment by Githook User [ 07/Mar/18 ]

Author:

{'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley', 'username': 'rozza'}

Message: Fix PojoCodecProvider handling of registered packages

Ensure registered packages and automatic codecs work in the
same manner.

JAVA-2799
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/7ae39a7d9ef4007a040e56f4455833e1814a1167

Comment by Ross Lawley [ 06/Mar/18 ]

PR: https://github.com/rozza/mongo-java-driver/pull/260

Comment by Ross Lawley [ 05/Mar/18 ]

Looking at the error message, it looks like the PojoCodec is not delegating to its internal Enum handling, for whatever reason.

Comment by Igal Rivin (Inactive) [ 05/Mar/18 ]

Hi ross.lawley, I tried that too.
The only thing that worked is taking out that enum from that package.

-Igal

Comment by Ross Lawley [ 05/Mar/18 ]

igal.rivin I think it may be an issue with nested Enums not being supported. Does splitting out the enum (so its not embedded in a class) fix the issue?

Generated at Thu Feb 08 08:58:06 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.