[JAVA-3286] @BsonIgnore doesn't always work Created: 11/May/19  Updated: 28/Oct/23  Resolved: 09/Aug/19

Status: Closed
Project: Java Driver
Component/s: BSON, POJO
Affects Version/s: 3.10.1
Fix Version/s: 3.11.0

Type: Bug Priority: Minor - P4
Reporter: Mike Baranczak Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I have a POJO that I want to save, with the following methods:

 

@BsonIgnore
public String getSecret() {
  return secret;
}
 
@BsonIgnore 
public void setSecret(String secret) { 
  this.secret = secret; 
} 
 
@BsonIgnore 
public void setSecret(Integer i) { 
  this.secret = ""+i; 
}

This causes an error: 

org.bson.codecs.configuration.CodecConfigurationException: Property 'secret' in TestListing, has differing data types: TypeData{type=String} and TypeData{type=Integer}

If I change the last method name to "setSecretAsInt", I get this error:

Unable to get value for property 'secretAsInt' in TestListing

I can make it work if I then add a dummy getter like this:

 

public Integer getSecretAsInt() { 
 return null; 
}

But that's just ugly, and shouldn't be necessary. If a property is marked as ignored, then the serialization code should ignore it, period.

Similar bug (already fixed): https://jira.mongodb.org/browse/JAVA-2620

 

 



 Comments   
Comment by Githook User [ 09/Aug/19 ]

Author:

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

Message: Delay the validation of PropertyModel type data

Now ensures the conventions have been applied before
validation takes place.

JAVA-3286
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/eb4a4796e7473173d402660c6d8b80d5ea186ac7

Comment by Ross Lawley [ 06/Aug/19 ]

PR: https://github.com/rozza/mongo-java-driver/pull/330

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