Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAVA-3815: Pojo Codec - Detect property models on extended interfaces #563

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Commits on Aug 14, 2020

  1. Pojo Codec: Detect property models on extended interfaces

    If you 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();
    }
    ```
    
    With a concrete implementation of SampleImplementor. 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",
    and not "first" or "second". 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` only is 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. We've worked around this problem for years
    and created 700+ hand written @BsonCreator annotations, but, enough is
    enough :-)
    
    This fix is relatively straight forward: Update `PojoBuilderHelper` to
    scan implementing classes and interfaces, which provides a fully
    populated property model.
    jflorencio committed Aug 14, 2020
    Configuration menu
    Copy the full SHA
    d7637d3 View commit details
    Browse the repository at this point in the history

Commits on Aug 18, 2020

  1. Configuration menu
    Copy the full SHA
    84010e7 View commit details
    Browse the repository at this point in the history