Skip to content

Fix query/update document reference computation for non id properties. #3856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3853-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3853-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3853-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3853-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ static class LinkageDocument {
private final String lookup;
private final org.bson.Document documentPointer;
private final Map<String, String> placeholderMap;
private final boolean isSimpleTargetPointer;

static LinkageDocument from(String lookup) {
return new LinkageDocument(lookup);
Expand All @@ -177,6 +178,7 @@ private LinkageDocument(String lookup) {
}

this.documentPointer = org.bson.Document.parse(targetLookup);
this.isSimpleTargetPointer = placeholderMap.size() == 1 && placeholderMap.containsValue("target") && lookup.contains("#target");
}

private String placeholder(int index) {
Expand All @@ -194,7 +196,7 @@ DocumentPointer<Object> getDocumentPointer(
propertyAccessor);
}

Document updatePlaceholders(org.bson.Document source, org.bson.Document target,
Object updatePlaceholders(org.bson.Document source, org.bson.Document target,
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext,
MongoPersistentEntity<?> persistentEntity, PersistentPropertyAccessor<?> propertyAccessor) {

Expand Down Expand Up @@ -245,6 +247,11 @@ Document updatePlaceholders(org.bson.Document source, org.bson.Document target,

target.put(entry.getKey(), entry.getValue());
}

if(target.size()==1 && isSimpleTargetPointer) {
return target.values().iterator().next();
}

return target;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ protected boolean isAssociationConversionNecessary(Field documentField, @Nullabl
return true;
}

if(property.isDocumentReference()) {
return true;
}

MongoPersistentEntity<?> entity = documentField.getPropertyEntity();
return entity.hasIdProperty()
&& (type.equals(DBRef.class) || entity.getRequiredIdProperty().getActualType().isAssignableFrom(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.FieldType;
import org.springframework.data.mongodb.core.mapping.MongoId;
Expand Down Expand Up @@ -424,6 +425,60 @@ void convertsNestedDBRefsCorrectly() {
assertThat(inClause.get(0)).isInstanceOf(com.mongodb.DBRef.class);
}

@Test // GH-3853
void convertsDocumentReferenceOnIdPropertyCorrectly() {

Sample reference = new Sample();
reference.foo = "s1";

Query query = query(where("sample").is(reference));
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedQuery).containsEntry("sample", "s1");
}

@Test // GH-3853
void convertsListDocumentReferenceOnIdPropertyCorrectly() {

Sample reference = new Sample();
reference.foo = "s1";

Query query = query(where("samples").is(Arrays.asList(reference)));
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedQuery).containsEntry("samples", Arrays.asList("s1"));
}

@Test // GH-3853
void convertsDocumentReferenceOnNonIdPropertyCorrectly() {

Customer reference = new Customer();
reference.id = new ObjectId();
reference.name = "c1";

Query query = query(where("customer").is(reference));
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedQuery).containsEntry("customer", "c1");
}

@Test // GH-3853
void convertsListDocumentReferenceOnNonIdPropertyCorrectly() {

Customer reference = new Customer();
reference.id = new ObjectId();
reference.name = "c1";

Query query = query(where("customers").is(Arrays.asList(reference)));
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedQuery).containsEntry("customers", Arrays.asList("c1"));
}

@Test // DATAMONGO-752
void mapsSimpleValuesStartingWith$Correctly() {

Expand Down Expand Up @@ -1496,6 +1551,25 @@ class WithMapDBRef {
@DBRef Map<String, Sample> mapWithDBRef;
}

static class WithDocumentReference {

private ObjectId id;

private String name;

@DocumentReference(lookup = "{ 'name' : ?#{#target} }") // remove `lookup` for the other test case.
private Customer customer;

@DocumentReference(lookup = "{ 'name' : ?#{#target} }") // remove `lookup` for the other test case.
private List<Customer> customers;

@DocumentReference
private Sample sample;

@DocumentReference
private List<Sample> samples;
}

class WithTextScoreProperty {

@Id String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.concurrent.atomic.AtomicInteger;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -49,6 +50,7 @@
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.DocumentTestUtils;
import org.springframework.data.mongodb.core.MongoExceptionTranslator;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.Unwrapped;
Expand Down Expand Up @@ -1251,6 +1253,64 @@ void multipleKeysStartingWithANumberInNestedPath() {
assertThat(mappedUpdate).isEqualTo("{\"$set\": {\"intKeyedMap.1a.map.0b\": \"testing\"}}");
}

@Test // GH-3853
void updateWithDocuRefOnId() {

Sample sample = new Sample();
sample.foo = "s1";

Update update = new Update().set("sample", sample);

Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("sample","s1")));
}

@Test // GH-3853
void updateListWithDocuRefOnId() {

Sample sample = new Sample();
sample.foo = "s1";

Update update = new Update().set("samples", Arrays.asList(sample));

Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("samples",Arrays.asList("s1"))));
}

@Test // GH-3853
void updateWithDocuRefOnProperty() {

Customer customer = new Customer();
customer.id = new ObjectId();
customer.name = "c-name";

Update update = new Update().set("customer", customer);

Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("customer","c-name")));
}

@Test // GH-3853
void updateListWithDocuRefOnProperty() {

Customer customer = new Customer();
customer.id = new ObjectId();
customer.name = "c-name";

Update update = new Update().set("customers", Arrays.asList(customer));

Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set",new org.bson.Document("customers", Arrays.asList("c-name"))));
}

static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
}
Expand Down Expand Up @@ -1621,4 +1681,35 @@ static class EntityWithNestedMap {
Map<String, Map<String, Map<String, Object>>> levelOne;
}

static class Customer {

@Id
private ObjectId id;
private String name;
}

static class Sample {

@Id private String foo;
}

static class WithDocumentReference {

private ObjectId id;

private String name;

@DocumentReference(lookup = "{ 'name' : ?#{#target} }") // remove `lookup` for the other test case.
private Customer customer;

@DocumentReference(lookup = "{ 'name' : ?#{#target} }") // remove `lookup` for the other test case.
private List<Customer> customers;

@DocumentReference
private Sample sample;

@DocumentReference
private List<Sample> samples;
}

}