-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Panache @BsonIgnore for methods #16399
Comments
/cc @FroMage, @loicmathieu |
Mmm, perhaps we drop the annotations somehow during transformation of the setter… |
Can you create a small reproducer project or at least provides the full stack trace ? @FroMage we do treat fields annotated via |
I would really like to share my project or make a sample project but I have so many issues and learnings due the new quarkus land that I am already overloaded... My Class: @MongoEntity(collection = "users")
public class User extends PanacheMongoEntityBase implements Comparable<User> {
@BsonId
@JsonProperty("id")
@BsonProperty("_id")
@Schema(example = "516448966")
private Long id = -1L;
[...]
@JsonProperty("last_activity")
@BsonProperty("last_activity")
private LocalDateTime lastActivity = getTime();
@JsonIgnore
@BsonIgnore
//TROUBLE METHOD CAUSES ERROR
public User setLastActivity(final LocalDateTime... lastActivities) {
setLastActivity(Arrays.stream(lastActivities).max(LocalDateTime::compareTo).orElse(getTimeLastYear()));
return this;
}
public LocalDateTime getLastActivity() {
return lastActivity;
}
public User setLastActivity(final LocalDateTime lastActivity) {
this.lastActivity = lastActivity;
return this;
}
public static List<User> dbFindById(final Iterable<?> entities) {
if (entities.iterator().hasNext()) {
return find("_id in ?1", entities).list();
}
return Collections.emptyList();
}
} The Error:
|
So I confirm I can reproduce. This works:
But if you comment out the method I get the same error. I can also confirm that the method has the right annotation:
So at this point I'm not sure why BSON is complaining. Note that in my case I don't actually have a helpful message:
|
Pushed a test at https://github.com/FroMage/quarkus/tree/16399, do you think you can figure out what BSON is complaining about @loicmathieu ? |
I suspect this is a limitation of the AutomaticPojoCodec that we use that mandates that getters/setters use the same dataType. |
So, searching the github repo is easy ;) This is indead a limitation of the PojoCodec, it check that the parameter class of the setter is the same than the class of the property (the field of your class): https://github.com/mongodb/mongo-java-driver/blob/5008b2a9445e477d563258eaaf4786f0f0ed83aa/bson/src/main/org/bson/codecs/pojo/PojoBuilderHelper.java#L151 So you can:
There is nothing we can do at our side, this is a limitation of the PojoCodec |
OK, so this is not a bug then? If not, can you add a note in our docs explaining not to do that, and what kind of exception it will cause? This way people can find it. |
I can have a note explaing that we are using the automatic pojo codec, that rules exists, and that if a I'm reluctant to specifically add this rule as it's not on the official documentation and we cannot list all the rules becasue they are written nowhere. In fact a third solution for @YunaBraska could be to open an issue on the mongodb-java-driver repo because its method is annotated by Anyway, adding some section about the pojo codec seems a good addition because we rely on it for MongoDB with Panache. |
When I rename the method then it's complaining that the field is not defined like this: Unable to get value for property 'lastActivities' in User A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type. |
@YunaBraska can you paste your updated class ? Maybe the fact that your return the User from the setter make the PojoCodec not considering your setter as a regular setter and generate an error. |
@loicmathieu its the same class as above... In that example I have the field Method: public void setLastActivities2(final LocalDateTime... lastActivities) {
setLastActivity(Arrays.stream(lastActivities).max(LocalDateTime::compareTo).orElse(getTimeLastYear()));
} Error:
|
I have finally found an ugly but simple workaround by not using words like |
I suggest you raised this issue at the Mongo Java driver side. |
@loicmathieu which project is this. I am a bit confused who does what in quarkus. |
This is not a Quarkus project but the official MongoDB Java Driver : https://github.com/mongodb/mongo-java-driver The issue is reproducible without Quarkus. |
@YunaBraska as the issue is not on Quarkus but on the MongoDB Java Driver can we close this issue ? |
@loicmathieu sorry for the late answer. I am really happy that you invested so much time and helped me in this issue :) I created a ticket on the mongo db driver but no one respond yet. I have no idea if it's possible. But I would expect and love if a framework could provide a workaround since its unclear if its getting fixed -_- |
@YunaBraska we will not twist the way the MongoDB driver works inside Quarkus, this is not a good thing to do. MongoDB with Panache uses the AutomaticPojoCodec to serialize your object in BSON. If the AutomaticPojoCodec cannot serialize your object in BSON (in your case, this is because it's no more a classic POJO/Bean but a domain object with more methods), you must provide a custom Codec for your class. I strongly suggest you create a MongoDB codec for your user class, it will be automatically discovered by Quarkus and registered inside the MongoDB driver codec registry, see https://quarkus.io/guides/mongodb#simplifying-mongodb-client-usage-using-bson-codec What I will do however, will be to add more information about the AutomaticPojoCodec inside the MongoDB with Panache guide and explaining that when a |
- Update link to the latest driver version - Improve wording - Add a section about the PojoCodecProvider Fixes quarkusio#16399
@YunaBraska I updated the guide with a section about the PojoCodecProvider and its limitation: #18010 There is nothing else we can do about your issue as it's a limitation on the PojoCodecProvider that is made for Java beans and mandates that all methods starting with set/get are getters/setters. |
Okay then you can close the ticket |
Thanks, closing it |
- Update link to the latest driver version - Improve wording - Add a section about the PojoCodecProvider Fixes quarkusio#16399
- Update link to the latest driver version - Improve wording - Add a section about the PojoCodecProvider Fixes quarkusio#16399 (cherry picked from commit dd6a4b0)
Describe the bug
Since Panache is more Domain Driven, I added also some methods with small logic which like convert a different input parameter e.g. Date to LocalDateTime or get max Date from an Array...
But for some reason
@BsonIgnore
only works on additional methods which are not setting fields.Expected behaviour
I expect that the method which I mark with
@BsonIgnore
gets actually ignoredActual behaviour
Example of a method which updates my
lastActivity
(LocalDateTime
) by the max parameter of anLocalDateTime[]
(Array)Error on save or get:
Error on save or get when renaming the method:
To Reproduce
@BsonIgnore
to itThe text was updated successfully, but these errors were encountered: