[JAVA-3935] PojoCodecProvider recognition of classes with discriminators only Created: 13/Jan/21  Updated: 05/Feb/21  Resolved: 05/Feb/21

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

Type: Bug Priority: Major - P3
Reporter: Alex Bevilacqua Assignee: Ross Lawley
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Zip Archive scratch.zip    
Issue Links:
Related
is related to JAVA-3707 Support package scanning for Discrimi... Backlog
is related to JAVA-4001 Automatic / package based PojoCodecs ... Backlog
Case:

 Description   

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type. exception can be raised when a class without properties (discriminator only) is a base class of one class as well as a nominal type in another class.



 Comments   
Comment by Ross Lawley [ 05/Feb/21 ]

Hi john.lilley@redpointglobal.com,

Thanks for the update. I'll close this ticket out, please follow JAVA-4001 for the fix for automatic / package based PojoCodecs.

All the best,

Ross

Comment by John Lilley [ 05/Feb/21 ]

I have verified that the workaround of useDiscriminator = true does fix the issue.

This is the comment left in my support ticket.  It may be relevant:

 

My name is **** and I'll be assisting my colleagues with this case.

This is done to provide a user-friendly getter that never returns null, while preserving the Mongodb getter that omits null fields. But my question is, why did this change matter, and is it a valid "fix" to the problem?

The root cause of the exception turns out to have nothing to do with the use of discriminators. Rather, it's that the PojoCodecProvider doesn't recognize classes with no properties as POJOs. Since SettingValue, the base class for SettingValueDashboard and also the nominal type of the value property in the SettingClass, has no properties, no Codec is found for that class and therefore an exception is thrown.

To work around this restriction, you can either

  • Add at least one property to SettingValue
  • Change the type of Setting.value to SettingValueDashboard
Comment by Ross Lawley [ 05/Feb/21 ]

I also added JAVA-4001 - which highlights the issue with automatic / package based PojoCodecs, where ClassModels don't contain any properties.

Ross

Comment by Ross Lawley [ 05/Feb/21 ]

Hi ross.lawley,

When using automatic / package based PojoCodecs can sometimes need help with discriminator lookups. We have JAVA-3707 to improve looking up discriminators.

Regarding the scratch repo, the issue in the Setting class @BsonProperty defaults to false for fields using discriminators - enabling it should fix your issue.

	@BsonProperty(value = "value", useDiscriminator = true)
	public final SettingValue getValue() { 
		return value;
	}

Ross

Comment by John Lilley [ 13/Jan/21 ]

The attached scratch.zip demonstrates the issue

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