[JAVA-4913] PojoBuilderHelper#configureClassModelBuilder is causing different output Created: 21/Mar/23  Updated: 19/Apr/23

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

Type: Bug Priority: Unknown
Reporter: Sergey Nikitin Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: Investigate our POJO implementation
Quarter: FY23Q4, FY24Q2
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Running this test code multiple times returns different results:

package org.bson.codecs.pojo;

import org.bson.codecs.pojo.annotations.BsonProperty;

public class Test {

    public static final String TEST_ID = "testId";
    public static final String BATCH_ID = "batchId";

    @BsonProperty(TEST_ID)
    private String testId;

    @BsonProperty(BATCH_ID)
    private String batchId;

    public Test(String testId, String batchId) {
        this.testId = testId;
        this.batchId = batchId;
    }

    public String getTestId() {
        return testId;
    }

    public void setTestId(String testId) {
        this.testId = testId;
    }

    public boolean isTestId() {
        return !testId.isEmpty();
    }

    public String getBatchId() {
        return batchId;
    }

    public void setBatchId(String batchId) {
        this.batchId = batchId;
    }

    public boolean isBatchId() {
        return !batchId.isEmpty();
    }

    public static void main(String[] args) {
            PropertyModelBuilder<?> property1 = ClassModel.builder(Test.class).getProperty(BATCH_ID);
            PropertyModelBuilder<?> property2 = ClassModel.builder(Test.class).getProperty(TEST_ID);
            System.out.println(((PropertyAccessorImpl) property1.getPropertyAccessor()).getPropertyMetadata().getError());
            System.out.println(((PropertyAccessorImpl) property2.getPropertyAccessor()).getPropertyMetadata().getError());
    }
}

Results:{{{}
Property 'batchId' in Test, has differing data types: TypeData{type=String} and TypeData{type=Boolean}.
null{}}}

 

{{null
Property 'testId' in Test, has differing data types: TypeData{type=String} and TypeData{type=Boolean}.}}

 

{{Property 'batchId' in Test, has differing data types: TypeData{type=String} and TypeData{type=Boolean}.
Property 'testId' in Test, has differing data types: TypeData{type=String} and TypeData{type=Boolean}.}}

 

{{null
null}}



 Comments   
Comment by Jeffrey Yemin [ 25/Mar/23 ]

Because the POJO codec follows the Java Beans standard of treating "is"-prefixed methods as boolean getter properties, so I suspect that's what's confusing it.

Comment by Sergey Nikitin [ 25/Mar/23 ]

It may not be reproducible in a loop, build is required each time. In this case an order of getters in the
getPropertyMethods is random. Right, changing "is" to other prefix is helpful, but what's JavaBeans got to do with it?

Comment by Jeffrey Yemin [ 25/Mar/23 ]

I can't reproduce this. I ran it 1000 times in a loop and it prints

null
null

every time. So not sure what's going on. But if you can't figure out what's different in your environment, it seems like a workaround would be to change isTestId/isBatchId to hasTestId/hasBatchId, as the "is" prefix does have special meaning in the Java Beans specification.

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