Skip to content

DATAMONGO-1998 - Fix Querydsl id handling for property references using ObjectId hex String representation. #567

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>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAMONGO-1998-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>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAMONGO-1998-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAMONGO-1998-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAMONGO-1998-SNAPSHOT</version>
</dependency>

<!-- reactive -->
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 @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAMONGO-1998-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>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATAMONGO-1998-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,30 @@ protected DBObject asDBObject(@Nullable String key, @Nullable Object value) {

value = value instanceof Optional ? ((Optional) value).orElse(null) : value;

if (ID_KEY.equals(key)) {
DBObject superIdValue = super.asDBObject(key, value);
Document mappedIdValue = mapper.getMappedObject((BasicDBObject) superIdValue, Optional.empty());
return (DBObject) JSON.parse(mappedIdValue.toJson());
if (key.endsWith(ID_KEY)) {
return convertId(key, value);
}

return super.asDBObject(key, value instanceof Pattern ? value : toQuerydslMongoType(value));
}

/**
* Convert a given, already known to be an {@literal id} or even a nested document id, value into the according id
* representation following the conversion rules of {@link QueryMapper#convertId(Object)}.
*
* @param key the property path to the given value.
* @param idValue the raw {@literal id} value.
* @return the {@literal id} representation in the required format.
*/
private DBObject convertId(String key, Object idValue) {

Object convertedId = mapper.convertId(idValue);

Document mappedIdValue = mapper.getMappedObject((BasicDBObject) super.asDBObject(key, convertedId),
Optional.empty());
return (DBObject) JSON.parse(mappedIdValue.toJson());
}

/*
* (non-Javadoc)
* @see com.querydsl.mongodb.MongodbSerializer#isReference(com.querydsl.core.types.Path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import lombok.Data;

import java.util.Arrays;

import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.Person;
import org.springframework.data.mongodb.repository.QPerson;
Expand All @@ -49,7 +54,9 @@ public class QuerydslRepositorySupportTests {
@Before
public void setUp() {

operations.remove(new Query(), Outer.class);
operations.remove(new Query(), Person.class);

person = new Person("Dave", "Matthews");
operations.save(person);

Expand Down Expand Up @@ -97,4 +104,54 @@ public void shouldAllowDbRefAgainstIdProperty() {
assertThat(queryUsingIdField.fetchOne(), equalTo(person));
assertThat(queryUsingIdField.fetchOne(), equalTo(queryUsingRefObject.fetchOne()));
}

@Test // DATAMONGO-1998
public void shouldLeaveStringIdThatIsNoValidObjectIdAsItIs() {

Outer outer = new Outer();
outer.id = "outer-1";
outer.inner = new Inner();
outer.inner.id = "inner-1";
outer.inner.value = "go climb a rock";

operations.save(outer);

QQuerydslRepositorySupportTests_Outer o = QQuerydslRepositorySupportTests_Outer.outer;
SpringDataMongodbQuery<Outer> query = repoSupport.from(o).where(o.inner.id.eq(outer.inner.id));

assertThat(query.fetchOne(), equalTo(outer));
}

@Test // DATAMONGO-1998
public void shouldConvertStringIdThatIsAValidObjectIdIntoTheSuch() {

Outer outer = new Outer();
outer.id = new ObjectId().toHexString();
outer.inner = new Inner();
outer.inner.id = new ObjectId().toHexString();
outer.inner.value = "eat sleep workout repeat";

operations.save(outer);

QQuerydslRepositorySupportTests_Outer o = QQuerydslRepositorySupportTests_Outer.outer;
SpringDataMongodbQuery<Outer> query = repoSupport.from(o).where(o.inner.id.eq(outer.inner.id));

assertThat(query.fetchOne(), equalTo(outer));
}

@Data
@Document
public static class Outer {

@Id String id;
Inner inner;
}

@Data
public static class Inner {

@Id String id;
String value;

}
}