Pojo Codec does not detect property models on extended interfaces

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Fixed
    • Priority: Major - P3
    • 5.6.0
    • Affects Version/s: 4.1.0
    • Component/s: POJO
    • None
    • Minor Change
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      I created a pull request, so copying the text from that:

      If we have an interface like:

      public interface SampleInterface {
       int getFirst();
       String getSecond();
      }
      

      which is implemented as:

      public abstract class SampleImplementor implements SampleInterface {
       public abstract boolean isThird();
      }
      

      and has a concrete implementation that's called `SampleImplementorImpl`.
      When `PojoBuilderHelper` goes to create the property models, it only checks
      for methods on the current class and super classes - not interfaces. In
      the above example, this means the property model will only have "third" entry -
      no "first" or "second" property model. Today, you can manually get around that by
      creating a @BsonCreator by hand:

      public abstract class SampleImplementor implements SampleInterface {
       @BsonCreator
       public static SampleImplementor newInstance(
       @BsonProperty("first") int first,
       @BsonProperty("second") String second,
       @BsonProperty("third") boolean third) {
       return new SampleImplementorImpl(first, second, third);
       }
      
      public abstract boolean isThird();
      }
      

      The presence of the `@BsonProperty` on the `@BsonCreator` method will
      create the property models. Conversely though, if you want to leverage a
      `Convention` implementation to dynamically create a `InstanceCreator`
      that knows how to find `SampleImplementorImpl` above, you won't be able
      to. `InstanceCreator` is only provided properties for which
      `PropertyModel` exists, so above, since `PojoBuilderHelper` didn't
      discover the interface fields, and there is no exposed API to add
      property models, your `InstanceCreator` will never be provided the
      `first` and `second` fields present on the interface.

      Simply put, if you provide the pojo codec a class that is not concrete,
      extends an interface for methods, and does not have a `@BsonCreator`
      annotation, there is no way to implement a `InstanceCreator`
      implementation that works for the non concrete class. You get stuck in
      a place where the class can serialize since the concrete implementation
      `SampleImplementorImpl` is provided at runtime, but then you have no way
      to deserialize it since usages in the code only reference `SampleImplementor`.

      We've worked around this problem for years and created 700+ hand written
      `@BsonCreator` annotations, so at this point I want to fix actual the problem.

      This fix is relatively straight forward: Update `PojoBuilderHelper` to
      scan implementing classes and interfaces, which provides a fully
      populated property model.

            Assignee:
            Ross Lawley
            Reporter:
            Joseph Florencio
            Ross Lawley
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: