Skip to content

Commit f82b791

Browse files
committed
DATAMONGO-1998 - Polishing.
Switch id field name check to equals or to match the last property path segment. Original pull request: #567.
1 parent 3785a52 commit f82b791

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Set;
2121
import java.util.regex.Pattern;
2222

23-
import com.mongodb.util.JSON;
2423
import org.springframework.data.mapping.context.MappingContext;
2524
import org.springframework.data.mongodb.core.convert.MongoConverter;
2625
import org.springframework.data.mongodb.core.convert.QueryMapper;
@@ -31,6 +30,7 @@
3130

3231
import com.mongodb.DBObject;
3332
import com.mongodb.DBRef;
33+
import com.mongodb.util.JSON;
3434
import com.querydsl.core.types.Constant;
3535
import com.querydsl.core.types.Expression;
3636
import com.querydsl.core.types.Operation;
@@ -116,7 +116,7 @@ protected String getKeyForPath(Path<?> expr, PathMetadata metadata) {
116116
@Override
117117
protected DBObject asDBObject(String key, Object value) {
118118

119-
if (key.endsWith(ID_KEY)) {
119+
if (ID_KEY.equals(key) || (key != null && key.endsWith("." + ID_KEY))) {
120120
return convertId(key, value);
121121
}
122122
return super.asDBObject(key, value instanceof Pattern ? value : converter.convertToMongoType(value));
@@ -134,8 +134,7 @@ private DBObject convertId(String key, Object idValue) {
134134

135135
Object convertedId = mapper.convertId(idValue);
136136

137-
DBObject mappedIdValue = mapper.getMappedObject(super.asDBObject(key, convertedId),
138-
null);
137+
DBObject mappedIdValue = mapper.getMappedObject(super.asDBObject(key, convertedId), null);
139138
return (DBObject) JSON.parse(JSON.serialize(mappedIdValue));
140139
}
141140

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.springframework.data.mongodb.core.DBObjectTestUtils.*;
2121

2222
import java.util.Collections;
23+
import java.util.List;
2324

2425
import org.bson.types.ObjectId;
2526
import org.hamcrest.Matchers;
@@ -50,7 +51,7 @@
5051

5152
/**
5253
* Unit tests for {@link SpringDataMongodbSerializer}.
53-
*
54+
*
5455
* @author Oliver Gierke
5556
* @author Christoph Strobl
5657
*/
@@ -158,9 +159,9 @@ public void shouldConvertCollectionOfObjectIdEvenWhenNestedInOperatorDbObject()
158159
DBObject serialized = serializer.asDBObject("_id", new BasicDBObject("$in", objectIds));
159160

160161
DBObject _id = getAsDBObject(serialized, "_id");
161-
Object[] $in = getTypedValue(_id, "$in", Object[].class);
162+
List<ObjectId> $in = getTypedValue(_id, "$in", List.class);
162163

163-
assertThat($in, Matchers.<Object> arrayContaining(firstId, secondId));
164+
assertThat($in, contains(firstId, secondId));
164165
}
165166

166167
@Test // DATAMONGO-1485

0 commit comments

Comments
 (0)