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 a54b91392e
commit d4505880c7
2 changed files with 18 additions and 1 deletions

View File

@@ -33,7 +33,6 @@ import org.bson.BsonValue;
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;
@@ -1099,6 +1098,11 @@ public class QueryMapper {
private PropertyPath forName(String path) {
try {
if (entity.getPersistentProperty(path) != null) {
return PropertyPath.from(Pattern.quote(path), entity.getTypeInformation());
}
return PropertyPath.from(path, entity.getTypeInformation());
} catch (PropertyReferenceException | InvalidPersistentPropertyPath e) {

View File

@@ -881,6 +881,15 @@ public class QueryMapperUnitTests {
assertThat(document).isEqualTo(new org.bson.Document("scripts", new Code(script)));
}
@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
public class Foo {
@Id private ObjectId id;
@@ -1013,4 +1022,8 @@ public class QueryMapperUnitTests {
@Field(targetType = FieldType.SCRIPT) //
List<String> scripts;
}
static class WithIdPropertyContainingUnderscore {
@Id String with_underscore;
}
}