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
- is related to
-
JAVA-5128 Add annotations to support default values for primitive types
- Backlog