[JAVA-4776] Support missing values for primitive types when decoding POJOs or records Created: 17/Oct/22  Updated: 27/Sep/23

Status: Backlog
Project: Java Driver
Component/s: POJO
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Quarter: FY24Q4
Documentation Changes Summary:

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?


 Description   

When decoding into either a record or a POJO with a BsonCreator-annotated constructor, and where there is a record component or constructor argument with primitive type, decoding a BSON document into the record or POJO will throw an exception if the BSON document is either missing a value for the corresponding field or if the value is null.

We should consider supporting this scenario in some way without throwing an exception. Options include:

  • Automatically using the default value (e.g. 0, false) for the primitive (this is actually the default behavior of the Jackson decoder).
  • Support an annotation on the component or field indicating that the default value should be used (or perhaps some other value)
  • Support multiple constructors instead of the single canonical constructor for records or the single BsonCreator-annotated constructor for POJOs.

Workarounds for the current behavior include:

  • For any fields that may not be included in the query response, make the corresponding record component or POJO field a reference type (e.g. Integer instead of int), so that it's nullable
  • Create new records or POJOs for each possible projection

Generated at Thu Feb 08 09:02:55 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.