[JAVA-4508] POJO codec unable to serialize instances of class with overloaded setter method Created: 25/Feb/22  Updated: 04/May/22

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

Type: Improvement Priority: Trivial - P5
Reporter: John Manko Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: external-user
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

I have a (superclass) that overloads a set method for one of it's properties.  It's simply for convenience.  The Java driver gets confused and can't differentiate the set method for the actual property type verse the signature for the alternate.

An example would be:

 

public class Dataset {
 
        @BsonProperty("property")
        protected List<String> property;
 
        public Dataset() {
        }
 
        @BsonCreator
        public Dataset(@BsonProperty("property") List<String> property) {
            this.property = property;
        }
 
        public List<String> getProperty() {
            return property;
        }
 
        public void setProperty(List<String> property) {
            this.property = property;
        }
 
        /**
         * This method is a problem.
         * @param property 
         */
 
        public void setProperty(String property) {
            if (this.property == null) {
                this.property = new ArrayList<>();
            }
            this.property.add(property);
        }
    }
}

 

 

Motivation

Who is the affected end user?

Anyone implementing PoJo classes with overloaded property methods.

How does this affect the end user?

BSON serialization fails.

How likely is it that this problem or use case will occur?

Edge case.

If the problem does occur, what are the consequences and how severe are they?

BSON serialization fails.

Is this issue urgent?

Not urgent.

Is this ticket required by a downstream team?

No.

Is this ticket only for tests?

No.



 Comments   
Comment by Valentin Kavalenka [ 01/Mar/22 ]

Hi mongodb@johnmanko.com,

Thank you for reporting the problem. The driver does not allow having setters that accept types not assignable from each other. In your situation those are List<String> and String, and neither is assignable from the other. However, since @BsonCreator is specified on a constructor that has a parameter marked with @BsonProperty("property"), the decoder does not need setters and does not use them when decoding. Therefore, it is reasonable to expect that any limitations on the setters are waived in this case. I agree that this will be a useful improvement.

To work around the issue, one may either change the name of the setter that was added for convenience, or introduce a helper method that converts the value and then uses the single available setter.

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