Skip to content

Commit f24e8e5

Browse files
mp911dechristophstrobl
authored andcommitted
Avoid nested Document conversion to primitive types for fields with an explicit write target.
We now no longer attempt to convert query Documents into primitive types to avoid e.g. Document to String conversion. Closes: #3783 Original Pull Request: #3797
1 parent bf86f39 commit f24e8e5

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,8 @@ protected boolean isKeyword(String candidate) {
778778
@Nullable
779779
private Object applyFieldTargetTypeHintToValue(Field documentField, @Nullable Object value) {
780780

781-
if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()) {
781+
if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()
782+
|| value instanceof Document || value instanceof DBObject) {
782783
return value;
783784
}
784785

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
import org.bson.types.ObjectId;
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
36-
import org.junit.jupiter.api.extension.ExtendWith;
37-
import org.mockito.junit.jupiter.MockitoExtension;
36+
3837
import org.springframework.core.convert.converter.Converter;
3938
import org.springframework.data.annotation.Id;
4039
import org.springframework.data.annotation.Transient;
@@ -83,9 +82,12 @@ public class QueryMapperUnitTests {
8382
@BeforeEach
8483
void beforeEach() {
8584

85+
MongoCustomConversions conversions = new MongoCustomConversions();
8686
this.context = new MongoMappingContext();
87+
this.context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
8788

8889
this.converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, context);
90+
this.converter.setCustomConversions(conversions);
8991
this.converter.afterPropertiesSet();
9092

9193
this.mapper = new QueryMapper(converter);
@@ -1335,6 +1337,25 @@ void mapStringIdFieldProjection() {
13351337
assertThat(mappedFields).containsEntry("_id", 1);
13361338
}
13371339

1340+
@Test // GH-3783
1341+
void retainsId$InWithStringArray() {
1342+
1343+
org.bson.Document mappedQuery = mapper.getMappedObject(
1344+
org.bson.Document.parse("{ _id : { $in: [\"5b8bedceb1e0bfc07b008828\"]}}"),
1345+
context.getPersistentEntity(WithExplicitStringId.class));
1346+
assertThat(mappedQuery.get("_id")).isEqualTo(org.bson.Document.parse("{ $in: [\"5b8bedceb1e0bfc07b008828\"]}"));
1347+
}
1348+
1349+
@Test // GH-3783
1350+
void mapsId$InInToObjectIds() {
1351+
1352+
org.bson.Document mappedQuery = mapper.getMappedObject(
1353+
org.bson.Document.parse("{ _id : { $in: [\"5b8bedceb1e0bfc07b008828\"]}}"),
1354+
context.getPersistentEntity(ClassWithDefaultId.class));
1355+
assertThat(mappedQuery.get("_id"))
1356+
.isEqualTo(org.bson.Document.parse("{ $in: [ {$oid: \"5b8bedceb1e0bfc07b008828\" } ]}"));
1357+
}
1358+
13381359
class WithDeepArrayNesting {
13391360

13401361
List<WithNestedArray> level0;
@@ -1404,6 +1425,12 @@ class WithStringId {
14041425
String name;
14051426
}
14061427

1428+
class WithExplicitStringId {
1429+
1430+
@MongoId(FieldType.STRING) String id;
1431+
String name;
1432+
}
1433+
14071434
class BigIntegerId {
14081435

14091436
@Id private BigInteger id;

0 commit comments

Comments
 (0)