[JAVA-3944] Bug while converting database attribute to POJO Created: 19/Jan/21  Updated: 21/Jan/21  Resolved: 21/Jan/21

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

Type: Bug Priority: Minor - P4
Reporter: Kunal jani Assignee: Ross Lawley
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Java MongoDB Using Reactivestreams for Storing DataBase Attributes as a POJO Object


Documentation Changes Summary:

 Description   

I have declared a variable private String transitNumber(using camel case) annotated with @BsonProperty("transit_number") in my POJO class. However, when I execute my query as in java MongoDB using reactive streams, the value of transitNumber = null is shown in the query result. But when I * declare my variable as *private TransitNumber transit_number(using snake case), the query result is being accurately returned. However, according to the Java conventions, all the variables must be declared in the camel case fornat. Can anyone explain to me why this happens? ** 

 ** 



 Comments   
Comment by Ross Lawley [ 21/Jan/21 ]

Hi kunaljani18@gmail.com,

I can't reproduce this issue. The following Pojo:

import org.bson.codecs.pojo.annotations.BsonProperty;
 
public class TransitPojo {
    @BsonProperty("transit_number")
    private String transitNumber;
 
    public TransitPojo() {
    }
 
    public TransitPojo(final String transitNumber) {
        this.transitNumber = transitNumber;
    }
 
    public String getTransitNumber() {
        return transitNumber;
    }
 
    public TransitPojo setTransitNumber(final String transitNumber) {
        this.transitNumber = transitNumber;
        return this;
    }
}

When using the following code adapted from the QuickTour it works as expected:

import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.codecs.pojo.PojoCodecProvider;
 
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
 
/**
 * The QuickTour code example
 */
public class QuickTour {
    /**
     * Run this main method to see the output of this quick example.
     *
     * @param args takes an optional single argument for the connection string
     */
    public static void main(final String[] args) {
        MongoClient mongoClient;
 
        if (args.length == 0) {
            // connect to the local database server
            mongoClient = MongoClients.create();
        } else {
            mongoClient = MongoClients.create(args[0]);
        }
 
        // get handle to "mydb" database
        MongoDatabase database = mongoClient.getDatabase("mydb");
 
 
        // get a handle to the "test" collection
        MongoCollection<Document> collection = database.getCollection("test");
        MongoCollection<TransitPojo> transitPojoCollection =
                collection.withCodecRegistry(fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
                        fromProviders(PojoCodecProvider.builder().automatic(true).build())))
                .withDocumentClass(TransitPojo.class);
 
        // drop all the data in it
        collection.drop();
        transitPojoCollection.insertOne(new TransitPojo("1234"));
 
        System.out.println(collection.find().first().toJson());
 
        // Clean up
        collection.drop();
 
        // release resources
        mongoClient.close();
    }
}

It outputs:

{"_id": {"$oid": "6009acaed654d802c58e8d5d"}, "transit_number": "1234"}

For that reason I'm closing this ticket as cannot reproduce. Should you be able to provide an example of it failing please comment on this ticket and I will investigate further.

Ross

Comment by Kunal jani [ 19/Jan/21 ]

Test case 1:

@BsonProperty("transit_number")

private String transitNumber;

Output: transitNumber = null;

Test case 2:

@BsonProperty("transit_number")

private String transit_number;

Output: transitNumber = "123456";

 

 

Comment by Ross Lawley [ 19/Jan/21 ]

Hi Kunal jani,

Many thanks for the ticket. Could you provide more detail on the bug? Either a full stacktrace or a test case to reproduce the error. Ideally, a minimal reproducible example would help as I could replicate the bug and use it as a test case for the fix.

Did you know the best place for asking questions is our MongoDB community portal, located here?

All the best,

Ross

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