Closed
Description
Using document references with non-Id properties in the context of queries and updates render invalid query documents. Consider the two examples below.
@DocumentReference(lookup = "{ 'name' : ?#{#target} }")
Query: {"customer": {"target": "wu"}, "sample": {"$oid": "615c04ae3fc147443f2a3352"}}
@DocumentReference(lookup = "{ '_id' : ?#{#target} }")
Query: {"customer": {"$oid": "615c04e09b072503508c6321"}, "sample": {"$oid": "615c04e09b072503508c6322"}}
Reproducer:
@Test
void mapsDocumentReferences() {
Customer customer = new Customer();
customer.id = new ObjectId();
customer.name = "wu";
Sample sample = new Sample();
sample.foo = new ObjectId().toString();
org.bson.Document mappedQuery = mapper.getMappedObject(
Criteria.where("customer").is(customer).and("sample").is(sample).getCriteriaObject(),
context.getPersistentEntity(WithReference.class));
// …
}
Object model
@Document
static class Customer {
@Id
private ObjectId id;
private String name;
private MyAddress address;
}
static class Sample {
@Id private String foo;
}
@Document
static class WithReference {
private ObjectId id;
private String name;
@DocumentReference(lookup = "{ 'name' : ?#{#target} }") // remove `lookup` for the other test case.
private Customer customer;
@DocumentReference
private Sample sample;
}