Skip to content

DATAMONGO-2221 - Fix mapping of Strings matching a valid ObjectId for unresolvable paths. #732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2221-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2221-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2221-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2221-SNAPSHOT</version>
</dependency>

<!-- reactive -->
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2221-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2221-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Example;
Expand Down Expand Up @@ -952,9 +951,7 @@ public boolean isIdField() {
MongoPersistentProperty idProperty = entity.getIdProperty();

if (idProperty != null) {

return name.equals(idProperty.getName()) || name.equals(idProperty.getFieldName())
|| name.endsWith("." + idProperty.getName()) || name.endsWith("." + idProperty.getFieldName());
return name.equals(idProperty.getName()) || name.equals(idProperty.getFieldName());
}

return DEFAULT_ID_NAMES.contains(name);
Expand Down Expand Up @@ -1042,16 +1039,15 @@ protected PersistentPropertyPath<MongoPersistentProperty> getPath() {
@Nullable
private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpression) {

try {
String rawPath = pathExpression.replaceAll("\\.\\d+", "") //
.replaceAll(POSITIONAL_OPERATOR.pattern(), "");

String rawPath = pathExpression.replaceAll("\\.\\d+", "") //
.replaceAll(POSITIONAL_OPERATOR.pattern(), "");

PropertyPath path = PropertyPath.from(rawPath, entity.getTypeInformation());
PropertyPath path = forName(rawPath);
if (path == null || isPathToJavaLangClassProperty(path)) {
return null;
}

if (isPathToJavaLangClassProperty(path)) {
return null;
}
try {

PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(path);

Expand All @@ -1073,7 +1069,30 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre
}

return propertyPath;
} catch (InvalidPersistentPropertyPath e) {
return null;
}
}

/**
* Querydsl happens to map id fields directly to {@literal _id} which breaks {@link PropertyPath} resolution. So if
* the first attempt fails we try to replace {@literal _id} with just {@literal id} and see if we can resolve if
* then.
*
* @param path
* @return the path or {@literal null}
*/
@Nullable
private PropertyPath forName(String path) {

try {
return PropertyPath.from(path, entity.getTypeInformation());
} catch (PropertyReferenceException | InvalidPersistentPropertyPath e) {

if (path.endsWith("_id")) {
return forName(path.substring(0, path.length() - 3) + "id");
}

return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,30 @@ public void shouldNotConvertHexStringToObjectIdForRenamedNestedIdField() {
assertThat(document).isEqualTo(new org.bson.Document("nested.id", idHex));
}

@Test // DATAMONGO-2221
public void shouldNotConvertHexStringToObjectIdForRenamedDeeplyNestedIdField() {

String idHex = new ObjectId().toHexString();
Query query = new Query(where("nested.deeplyNested.id").is(idHex));

org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(RootForClassWithExplicitlyRenamedIdField.class));

assertThat(document).isEqualTo(new org.bson.Document("nested.deeplyNested.id", idHex));
}

@Test // DATAMONGO-2221
public void shouldNotConvertHexStringToObjectIdForUnresolvablePath() {

String idHex = new ObjectId().toHexString();
Query query = new Query(where("nested.unresolvablePath.id").is(idHex));

org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(RootForClassWithExplicitlyRenamedIdField.class));

assertThat(document).isEqualTo(new org.bson.Document("nested.unresolvablePath.id", idHex));
}

@Document
public class Foo {
@Id private ObjectId id;
Expand Down Expand Up @@ -926,6 +950,11 @@ static class RootForClassWithExplicitlyRenamedIdField {
static class ClassWithExplicitlyRenamedField {

@Field("id") String id;
DeeplyNestedClassWithExplicitlyRenamedField deeplyNested;
}

static class DeeplyNestedClassWithExplicitlyRenamedField {
@Field("id") String id;
}

static class ClassWithGeoTypes {
Expand Down