Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
jflorencio wants to merge2 commits intomongodb:main
base:main
Choose a base branch
Loading
fromjflorencio:pojo-codec-interfaces-master

Conversation

jflorencio
Copy link
Contributor

@jflorenciojflorencio commentedAug 14, 2020
edited by jyemin
Loading

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 calledSampleImplementorImpl.
WhenPojoBuilderHelper 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 aInstanceCreator
that knows how to findSampleImplementorImpl above, you won't be able
to.InstanceCreator is only provided properties for which
PropertyModel exists, so above, sincePojoBuilderHelper didn't
discover the interface fields, and there is no exposed API to add
property models, yourInstanceCreator will never be provided the
first andsecond 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 aInstanceCreator
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 referenceSampleImplementor.

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: UpdatePojoBuilderHelper to
scan implementing classes and interfaces, which provides a fully
populated property model.

JAVA-3815

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 checksfor methods on the current class, and super classes - not interfaces. Inthe above example, this means the property model will only have "third",and not "first" or "second". Today, you can manually get around that bycreating 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 willcreate the property models. Conversely though, if you want to leverage aConvention implementation to dynamically create a `InstanceCreator`that knows how to find `SampleImplementorImpl` above, you won't be ableto. `InstanceCreator` only is provided properties for which`PropertyModel` exists, so above, since `PojoBuilderHelper` didn'tdiscover the interface fields, and there is no exposed API to addproperty 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 @BsonCreatorannotation, there is no way to implement a InstanceCreatorimplementation that works. We've worked around this problem for yearsand created 700+ hand written @BsonCreator annotations, but, enough isenough :-)This fix is relatively straight forward: Update `PojoBuilderHelper` toscan implementing classes and interfaces, which provides a fullypopulated property model.
@jflorencio
Copy link
ContributorAuthor

Is there anything I can do to help make progress on this?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@jflorencio@jyemin

[8]ページ先頭

©2009-2025 Movatter.jp