Skip to content

Commit 24e06cf

Browse files
committed
DATAMONGO-761 - Fix path key lookup for non-properties in SpringDataMongoDBSerializer.
In our Querydsl MongodbSerializer implementation we now only inspect the MongoPersistentProperty for a field name if the given path is really a property path. Previously we tried to always resolve a persistent property even if the given path was an array index path, a map key or the like.
1 parent 1b83ff0 commit 24e06cf

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.mysema.query.mongodb.MongodbSerializer;
2929
import com.mysema.query.types.Path;
3030
import com.mysema.query.types.PathMetadata;
31+
import com.mysema.query.types.PathType;
3132

3233
/**
3334
* Custom {@link MongodbSerializer} to take mapping information into account when building keys for constraints.
@@ -61,6 +62,10 @@ public SpringDataMongodbSerializer(MongoConverter converter) {
6162
@Override
6263
protected String getKeyForPath(Path<?> expr, PathMetadata<?> metadata) {
6364

65+
if (!metadata.getPathType().equals(PathType.PROPERTY)) {
66+
return super.getKeyForPath(expr, metadata);
67+
}
68+
6469
Path<?> parent = metadata.getParent();
6570
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(parent.getType());
6671
MongoPersistentProperty property = entity.getPersistentProperty(metadata.getName());

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

+17-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.mongodb.DBObject;
3737
import com.mysema.query.types.expr.BooleanOperation;
3838
import com.mysema.query.types.path.PathBuilder;
39+
import com.mysema.query.types.path.SimplePath;
3940
import com.mysema.query.types.path.StringPath;
4041

4142
/**
@@ -46,8 +47,7 @@
4647
@RunWith(MockitoJUnitRunner.class)
4748
public class SpringDataMongodbSerializerUnitTests {
4849

49-
@Mock
50-
MongoDbFactory dbFactory;
50+
@Mock MongoDbFactory dbFactory;
5151
MongoConverter converter;
5252
SpringDataMongodbSerializer serializer;
5353

@@ -117,10 +117,23 @@ public void convertsIdPropertyCorrectly() {
117117
assertThat(result.get("_id"), is((Object) id));
118118
}
119119

120+
/**
121+
* @see DATAMONGO-761
122+
*/
123+
@Test
124+
public void looksUpKeyForNonPropertyPath() {
125+
126+
PathBuilder<Address> builder = new PathBuilder<Address>(Address.class, "address");
127+
SimplePath<Object> firstElementPath = builder.getArray("foo", String[].class).get(0);
128+
String path = serializer.getKeyForPath(firstElementPath, firstElementPath.getMetadata());
129+
130+
assertThat(path, is("0"));
131+
}
132+
120133
class Address {
121134
String id;
122135
String street;
123-
@Field("zip_code")
124-
String zipCode;
136+
@Field("zip_code") String zipCode;
137+
@Field("bar") String[] foo;
125138
}
126139
}

0 commit comments

Comments
 (0)