Skip to content

Commit fadca10

Browse files
christophstroblmp911de
authored andcommitted
Support @DocumentReference via Querydsl.
Closes #4037 Original pull request: #4069.
1 parent 4032013 commit fadca10

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.regex.Pattern;
2323

2424
import org.bson.Document;
25-
2625
import org.springframework.data.mapping.context.MappingContext;
2726
import org.springframework.data.mongodb.core.convert.MongoConverter;
2827
import org.springframework.data.mongodb.core.convert.QueryMapper;
@@ -158,8 +157,20 @@ protected Object convert(@Nullable Path<?> path, @Nullable Constant<?> constant)
158157

159158
MongoPersistentProperty property = getPropertyFor(path);
160159

161-
return property.isIdProperty() ? asReference(constant.getConstant(), path.getMetadata().getParent())
162-
: asReference(constant.getConstant(), path);
160+
if (property.isDocumentReference()) {
161+
return converter.toDocumentPointer(constant.getConstant(), property).getPointer();
162+
}
163+
164+
if (property.isIdProperty()) {
165+
166+
MongoPersistentProperty propertyForPotentialDbRef = getPropertyForPotentialDbRef(path);
167+
if (propertyForPotentialDbRef != null && propertyForPotentialDbRef.isDocumentReference()) {
168+
return converter.toDocumentPointer(constant.getConstant(), propertyForPotentialDbRef).getPointer();
169+
}
170+
return asReference(constant.getConstant(), path.getMetadata().getParent());
171+
}
172+
173+
return asReference(constant.getConstant(), path);
163174
}
164175

165176
@Nullable

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.querydsl.core.types.dsl.PathBuilder;
5151
import com.querydsl.core.types.dsl.SimplePath;
5252
import com.querydsl.core.types.dsl.StringPath;
53+
import org.springframework.data.mongodb.repository.User;
5354

5455
/**
5556
* Unit tests for {@link SpringDataMongodbSerializer}.
@@ -225,6 +226,26 @@ void chainMultipleAndFlattensCorrectly() {
225226
assertThat(serializer.handle(testExpression)).isEqualTo(expected);
226227
}
227228

229+
@Test // GH-4037
230+
void parsesDocumentReference() {
231+
232+
User user = new User();
233+
user.setId("007");
234+
Predicate predicate = QPerson.person.spiritAnimal.eq(user);
235+
236+
assertThat(serializer.handle(predicate)).isEqualTo(Document.parse("{ 'spiritAnimal' : '007' }"));
237+
}
238+
239+
@Test // GH-4037
240+
void parsesDocumentReferenceOnId() {
241+
242+
User user = new User();
243+
user.setId("007");
244+
Predicate predicate = QPerson.person.spiritAnimal.id.eq("007");
245+
246+
assertThat(serializer.handle(predicate)).isEqualTo(Document.parse("{ 'spiritAnimal' : '007' }"));
247+
}
248+
228249
class Address {
229250
String id;
230251
String street;

0 commit comments

Comments
 (0)