Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2799

Enum is handled as POJO when coexists in the same package as the rest of the POJO classes

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.6.4, 3.7.0
    • Affects Version/s: 3.6.3
    • Component/s: Codecs
    • None

      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

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            igal.rivin@mongodb.com Igal Rivin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: