DATAMONGO-2339 - Fix QueryMapper field name resolution for properties containing underscore.

We now prevent splitting of paths that contain underscores if the entity contains a property that matches.

Original pull request: #777.
This commit is contained in:
Christoph Strobl
2019-08-09 10:12:35 +02:00
committed by Mark Paluch
parent 564acd75d5
commit 4be53ac952
2 changed files with 20 additions and 0 deletions

View File

@@ -26,6 +26,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
import org.bson.BsonValue; import org.bson.BsonValue;
import org.bson.Document; import org.bson.Document;
@@ -1064,6 +1065,11 @@ public class QueryMapper {
private PropertyPath forName(String path) { private PropertyPath forName(String path) {
try { try {
if (entity.getPersistentProperty(path) != null) {
return PropertyPath.from(Pattern.quote(path), entity.getTypeInformation());
}
return PropertyPath.from(path, entity.getTypeInformation()); return PropertyPath.from(path, entity.getTypeInformation());
} catch (PropertyReferenceException | InvalidPersistentPropertyPath e) { } catch (PropertyReferenceException | InvalidPersistentPropertyPath e) {

View File

@@ -28,6 +28,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import org.bson.types.Code;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -856,6 +857,15 @@ public class QueryMapperUnitTests {
assertThat(document).isEqualTo(new org.bson.Document("nested.unresolvablePath.id", idHex)); assertThat(document).isEqualTo(new org.bson.Document("nested.unresolvablePath.id", idHex));
} }
@Test // DATAMONGO-2339
public void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly() {
org.bson.Document target = mapper.getMappedObject(new org.bson.Document("with_underscore", "id-1"),
context.getPersistentEntity(WithIdPropertyContainingUnderscore.class));
assertThat(target).isEqualTo(new org.bson.Document("_id", "id-1"));
}
@Document @Document
public class Foo { public class Foo {
@Id private ObjectId id; @Id private ObjectId id;
@@ -979,4 +989,8 @@ public class QueryMapperUnitTests {
static class EntityWithComplexValueTypeList { static class EntityWithComplexValueTypeList {
List<SimpeEntityWithoutId> list; List<SimpeEntityWithoutId> list;
} }
static class WithIdPropertyContainingUnderscore {
@Id String with_underscore;
}
} }