[JAVA-2653] POJO that implements a generic interface cannot be serialized Created: 06/Nov/17  Updated: 29/Oct/23  Resolved: 04/Dec/17

Status: Closed
Project: Java Driver
Component/s: POJO
Affects Version/s: 3.5.0
Fix Version/s: 3.6.0

Type: Bug Priority: Major - P3
Reporter: Zoe Snape Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File issue_demo.tar.gz    
Issue Links:
Related
related to JAVA-2620 @BsonIgnore doesn't prevent PojoBuild... Closed

 Description   

I ran into a problem with POJO classes that implement a generic interface. Basically, the getDeclaredMethods function used in PojoBuilderHelper returns signatures for both the generic property type and the actual property type and then it gets upset because it thinks there are two methods with the same name and different types. I've attached a minimal example that demonstrates this issue. Unfortunately, due to JAVA-2620, adding @BsonIgnore to the property/methods in question doesn't fix this.



 Comments   
Comment by Githook User [ 04/Dec/17 ]

Author:

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

Message: PojoCodec - Added test for generic interfaces

JAVA-2653
Branch: 3.6.x
https://github.com/mongodb/mongo-java-driver/commit/8b351183734c1a2db7bcc9e413d465eac6a99311

Comment by Ross Lawley [ 04/Dec/17 ]

This was fixed as part of other PojoCodec updates.

Comment by Githook User [ 04/Dec/17 ]

Author:

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

Message: PojoCodec - Added test for generic interfaces

JAVA-2653
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/8b351183734c1a2db7bcc9e413d465eac6a99311

Comment by Jeffrey Yemin [ 06/Nov/17 ]

So that others don't have to download the attached file, I'm pasting the code here as well:

public class MyClass implements MyInterface<Long> {
 
  private Long m_property;
 
  public Long getProperty() {
    return m_property;
  }
 
  public void setProperty(Long property) {
    m_property = property;
  }
 
}
 
public interface MyInterface<X extends Serializable> {
 
  X getProperty();
 
  void setProperty(X property);
}
 
public class Main {
 
  public static void main(String[] args) {
 
    // set up POJO Codec Provider/Registry
    CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build();
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClient.getDefaultCodecRegistry(), fromProviders(pojoCodecProvider));
    MongoCollection<MyClass> myCollection = new MongoClient().getDatabase("test").getCollection("test", MyClass.class);
    myCollection = myCollection.withCodecRegistry(pojoCodecRegistry);
 
    // enumerate declared methods
    Method[] myMethods = MyClass.class.getDeclaredMethods();
    for (Method method : myMethods) {
      System.out.println("Found declared method: " + method.toString());
    }
 
    // attempt an insert
    MyClass exampleObject = new MyClass();
    exampleObject.setProperty(new Date().getTime());
    try {
      myCollection.insertOne(exampleObject);
    } catch (Exception e) {
      System.out.println("Caught an exception on insert!");
      e.printStackTrace();
    }
  }
 
}

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