[JAVA-4833] Using @BsonId in a @BsonCreator constructor has confusing behavior Created: 27/Dec/22 Updated: 12/Jan/23 Resolved: 12/Jan/23 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Question | Priority: | Major - P3 |
| Reporter: | Andrew Marshall | Assignee: | Ross Lawley |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Description |
|
Java Driver Version: 4.7 Why is there a difference in behavior between using @BsonProperty("_id") and @BsonId in the constructor of a model annotated with @BsonCreator?
I was getting the following error:
Document from the DB
Model's constructor
Notably, the model did not have any getters or settings on the ID field. Permission was defined as Model with the standard constructor using @BsonProperty annotations for each field in the document above
When I updated the constructor to the following I no longer saw any issues
In addition adding any of the following things while using @BsonId in the constructor also worked
Looking at the java driver's documentation is says you need to have all parameters in a @BsonCreator constructor, but it looks like that is not the case as well: https://www.mongodb.com/docs/drivers/java/sync/current/fundamentals/data-formats/pojo-customization/#annotations
Also if there is a desire to have no functional difference between @BsonId and @BsonProperty("_id") it would be good to address the current gap in functionality |
| Comments |
| Comment by Andrew Marshall [ 12/Jan/23 ] |
|
Hi Ross! Thanks for the detailed response! Great to hear that the issue was able to be updated and that you flagged the docs to be updated as well. Feel free to close this out and I can watch the ticket you linked here for any updates. |
| Comment by Ross Lawley [ 12/Jan/23 ] |
|
Hi andrew.marshall@mongodb.com, Apologies for jumping to conclusions in my initial response - I had forgotten the work done in This turns out to be more of a subtle bug than I initially realized - I've updated Please follow
I says you need to have all parameters in the @BsonCreator annotated with @BsonProperty. Which is not quite correct they need to be annotated with either @BsonProperty or @BsonId - so I've added DOCSP-27400 to get that updated. All the best, Ross |
| Comment by Ross Lawley [ 12/Jan/23 ] |
|
Hi andrew.marshall@mongodb.com,
Apologies, looks like I was incorrect here, looks there may be a bug in the CreatorExecutable (the code that handles BsonCreator). Investigating further, now trying to reproduce the error. Ross |
| Comment by Ross Lawley [ 12/Jan/23 ] |
|
Hi andrew.marshall@mongodb.com, Thanks for the ticket - good questions and a good point. @BsonId marks a property as the id. Iirc it was added for users who may not know the MongoDB uses _id as the id field.
There should be none and functionally it should be the same, the scenario of using with @BsonCreator wasn't thought of at the time of development. I've added Feel free to reach out on slack for general java driver questions #java as we prefer internal questions there or public questions out in the community forums. Ross |