Hi,
I am needing to create a Quarkus application that uses the MongoDB Panache module.
The issue is that our MongoDB & Collections already exist and can't be changed. Several of our collections have both an _id & an id field and this is apparently not allowed/supported with the current Quarkus MongoDB Panache implementation.
Our collections are like this:
public class MyMongoEntity extends PanacheMongoEntityBase {
@BsonId
public ObjectId _id;
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
...
}
Which generates errors like this:
org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'MyMongoEntity' failed with the following exception:
Failed to decode 'MyMongoEntity'. Decoding 'id' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.
I believe the problem is in this Quarkus jar: io.quarkus:quarkus-mongodb-client:2.16.4.Final
Specifically in the MongoClients#configureCodecRegistry method it has this bit of code:
PojoCodecProvider.Builder pojoCodecProviderBuilder = PojoCodecProvider.builder()
.automatic(true)
.conventions(Conventions.DEFAULT_CONVENTIONS);
The problem is the fixed Conventions.DEFAULT_CONVENTIONS used. This is defined as:
public static final List<Convention> DEFAULT_CONVENTIONS =
unmodifiableList(asList(CLASS_AND_PROPERTY_CONVENTION, ANNOTATION_CONVENTION, OBJECT_ID_GENERATORS));
This code treats the id field as the BsonId and it ignores the _id field.
The CLASS_AND_PROPERTY_CONVENTION is the problem we don't want that for our use case. I believe that if I could configure this as asList(ANNOTATION_CONVENTION, OBJECT_ID_GENERATORS) it would work for our MongoDb use case.
Could this be made configurable? I can't find any workarounds for this issue so it's blocking us from using Quarkus MongoDb with Panache.
If there is a workaround can someone advise?
Thanks,
-David