Skip to content

Commit f7cf235

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

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;
@@ -186,8 +185,20 @@ protected Object convert(@Nullable Path<?> path, @Nullable Constant<?> constant)
186185

187186
MongoPersistentProperty property = getPropertyFor(path);
188187

189-
return property.isIdProperty() ? asReference(constant.getConstant(), path.getMetadata().getParent())
190-
: asReference(constant.getConstant(), path);
188+
if (property.isDocumentReference()) {
189+
return converter.toDocumentPointer(constant.getConstant(), property).getPointer();
190+
}
191+
192+
if (property.isIdProperty()) {
193+
194+
MongoPersistentProperty propertyForPotentialDbRef = getPropertyForPotentialDbRef(path);
195+
if (propertyForPotentialDbRef != null && propertyForPotentialDbRef.isDocumentReference()) {
196+
return converter.toDocumentPointer(constant.getConstant(), propertyForPotentialDbRef).getPointer();
197+
}
198+
return asReference(constant.getConstant(), path.getMetadata().getParent());
199+
}
200+
201+
return asReference(constant.getConstant(), path);
191202
}
192203

193204
@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)