[JAVA-4916] BsonDiscriminator key not populated on reads Created: 23/Mar/23  Updated: 27/Oct/23  Resolved: 11/Apr/23

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

Type: Question Priority: Unknown
Reporter: Darren Matthews Assignee: Ross Lawley
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

When reading a POJO that has been inserted using the BsonDiscriminator annotation the object maps correctly to the java object but the key value is not populated in the object. In the extended class I would have expected at least the 'value' part of the annotation would have populated the key field with the value.

 

Server Version : 5.0.15

Driver Version: mongodb-driver-sync @ 4.9.0

 

@BsonDiscriminator(key = "type")
public abstract class Action {
    ActionType type;
} 

@BsonDiscriminator(key = "type", value = "SHOT") 
public class Shot extends Action {
// Some other fields that successfully populate
}



 Comments   
Comment by PM Bot [ 11/Apr/23 ]

There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information.

Comment by PM Bot [ 04/Apr/23 ]

Hey darrenmatthews776@googlemail.com, We need additional details to investigate the problem. If this is still an issue for you, please provide the requested information.

Comment by Ross Lawley [ 28/Mar/23 ]

Hi darrenmatthews776@googlemail.com,

Thanks for the ticket. The discriminator field is considered internal metadata used to help the PojoCodec determine which class to use as the concrete instance.

There may be some annotations or conventions that you can use (either existing or custom) that would allow it to be used as a field value. However, the expectation is that it is internal data aside from the POJO data.

All the best,

Ross

Comment by Darren Matthews [ 23/Mar/23 ]

Adding a default constructor into the child works i.e.

@BsonDiscriminator(key = "type", value = "SHOT") 
public class Shot extends Action {
  
   @BsonCreator
   public Shot() {
       this.type = ActionType.SHOT;
   }
 
   // Some other fields that successfully populate
} 

 My expectation is that is should just work (it might because im using an ENUM instead of a string?)

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