Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4776

Support missing values for primitive types when decoding POJOs or records

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: POJO
    • None
    • Hide

      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?

      Show
      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?

      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

            Assignee:
            Unassigned Unassigned
            Reporter:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: