[JAVA-4669] BSON Ignore in POJO Codec not working as expected on methods Created: 05/Jul/22  Updated: 27/Oct/23  Resolved: 21/Jul/22

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

Type: Bug Priority: Minor - P4
Reporter: Dennis Lee Assignee: Ross Lawley
Resolution: Works as Designed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to JAVA-4479 Issue with setter of different type t... Backlog

 Description   

Summary

When using codecs with a POJO, any extra methods beginning with the keyword get or set will trigger exceptions, even if the method is marked with the @BsonIgnore annotation.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

4.6.1

How to Reproduce

Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

public class User {
    @BsonId
    private ObjectId mongoID;

    @BsonIgnore
    public User setMongoID (String mongoID) {
        this.mongoID = new ObjectId(mongoID);
        return this;
    }
    public User setMongoID (ObjectId mongoID) {
        this.mongoID = mongoID;
        return this;
    }
    public ObjectId getMongoID() {
        return mongoID;
    }
}

 

Additional Background

Please provide any additional background information that may be helpful in diagnosing the bug.



 Comments   
Comment by Ross Lawley [ 21/Jul/22 ]

Hi dennis@nornir.io,

Not sure I follow - the exceptions I think you're seeing is because the POJO has incompatible types for the mongoID property and the PojoCodec doesn't support overloads for properties.

It looks like you want to use the mongoID property as the _id as it has a @bsonId annotation. But you also declare you want to ignore the mongoID property because a setter for it has the @bsonIgnore annotation, which isn't supported (as overloads aren't supported at this time). So the work around would be either to: Use the ObjectId constructor and have a single setter, create an alternative setter and ignore that or create a custom codec.

Ross

Comment by Dennis Lee [ 21/Jul/22 ]

Hi Ross,

Wait, so the solution to being able to have more get and set methods in the POJO is to fill it with unused junk variables?

Comment by Ross Lawley [ 21/Jul/22 ]

Hi dennis@nornir.io,

The annotations such as BsonIgnore are used as part of a conventions and are processed after the initial building of the PropertyModel.

Please note: BsonIgnore is used to ignore a whole property - which in the above case would be mapped to mongoId. My suggestions would be to add a new property eg setMongoIDFromString and ignore the alternative property.

maxsimusi1993@gmail.com those errors can be related to something else and can often be fixed by explicitly configuring a PojoCodec.

Ross

Comment by Maxim Fenixov [ 21/Jul/22 ]

Can confirm for 4.6.1 and 3.12.11, it usually leads to the exception like:

libs.bson.codecs.configuration.CodecConfigurationException:
An exception occurred when encoding using the AutomaticPojoCodec.Encoding a RequestEntity: 'RequestEntity{id=62d9341a1dc99860a2d3f0f6, <REDACTED>}' failed with the following exception:
 
Unable to get value for property 'rateEntry' in RequestEntity
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at libs.bson.codecs.pojo.AutomaticPojoCodec.encode(AutomaticPojoCodec.java:53)

 

 

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