[JAVA-2620] @BsonIgnore doesn't prevent PojoBuilderHelper.configureClassModelBuilder from calling getTypeData on ignored fields Created: 04/Oct/17  Updated: 29/Oct/23  Resolved: 04/Dec/17

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

Type: Bug Priority: Major - P3
Reporter: Kieron Edwards Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to JAVA-2653 POJO that implements a generic interf... Closed

 Description   

In a POJO if you have a field or method that is marked with @BsonIgnore , and if this is a type disallowed by the TypeData$Builder the class model creation will fail - despite this field / method not being required.

Example exception

java.lang.IllegalStateException: Invalid Map type. Maps MUST have string keys, found class com.TestEnum instead.
	at org.bson.codecs.pojo.TypeData$Builder.validate(TypeData.java:117)
	at org.bson.codecs.pojo.TypeData$Builder.build(TypeData.java:104)
	at org.bson.codecs.pojo.PojoBuilderHelper.getTypeData(PojoBuilderHelper.java:158)
	at org.bson.codecs.pojo.PojoBuilderHelper.configureClassModelBuilder(PojoBuilderHelper.java:87)
	at org.bson.codecs.pojo.ClassModelBuilder.<init>(ClassModelBuilder.java:59)
	at org.bson.codecs.pojo.ClassModel.builder(ClassModel.java:63)
	at org.bson.codecs.pojo.PojoCodecProvider.createClassModel(PojoCodecProvider.java:194)
	at org.bson.codecs.pojo.PojoCodecProvider.access$100(PojoCodecProvider.java:38)
	at org.bson.codecs.pojo.PojoCodecProvider$Builder.build(PojoCodecProvider.java:115)

Here the pojo contains a Map keyed on an enum. This map is marked with @BsonIgnore, hence I would not expect the failure above.



 Comments   
Comment by Christian H. [ 03/Feb/21 ]

I'd also love to be able to use enums as map keys

Comment by Githook User [ 04/Dec/17 ]

Author:

{'username': 'rozza', 'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley'}

Message: TypeData no longer validates data.

With the introduction of PropertyCodecProviders the
TypeData class no longer should validate the data it sees
for Collections or Maps.

This changes behavior for "invalid Maps" to a possible
runtime CodecConfigurationException. However, it
allows users to register their own PropertyCodecProviders
to handle these types if desired.

The side effect of this change is @BsonIgnore and
conventions can now be used to ignore properties
regardless of validity.

JAVA-2620
Branch: 3.6.x
https://github.com/mongodb/mongo-java-driver/commit/766a6003fbb8030ac5c3fde972e7410f0dc0d812

Comment by Githook User [ 04/Dec/17 ]

Author:

{'username': 'rozza', 'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley'}

Message: TypeData no longer validates data.

With the introduction of PropertyCodecProviders the
TypeData class no longer should validate the data it sees
for Collections or Maps.

This changes behavior for "invalid Maps" to a possible
runtime CodecConfigurationException. However, it
allows users to register their own PropertyCodecProviders
to handle these types if desired.

The side effect of this change is @BsonIgnore and
conventions can now be used to ignore properties
regardless of validity.

JAVA-2620
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/766a6003fbb8030ac5c3fde972e7410f0dc0d812

Comment by Ross Lawley [ 05/Oct/17 ]

Thanks edwaki,

Interesting case, the issue here is Conventions such as the AnnotationConvention are only applied after the initial mapping of the POJO. Here the error is occurring during the initial mapping.

I'll have to consider how best to handle scenarios like this in the future.

Ross

Generated at Thu Feb 08 08:57:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.