Fix conversion of regular expression in queries.
This commit fixes an issue where patterns targeting id properties might have been falsely converted into the properties type, turning a Pattern into it's string representation. Closes #4674 Original pull request: #4718
This commit is contained in:
committed by
Mark Paluch
parent
e586b19d63
commit
c49ee2534e
@@ -815,6 +815,11 @@ public class QueryMapper {
|
||||
*/
|
||||
@Nullable
|
||||
public Object convertId(@Nullable Object id, Class<?> targetType) {
|
||||
|
||||
if (!SpecialTypeTreatment.INSTANCE.isConversionCandidate(id)) {
|
||||
return id;
|
||||
}
|
||||
|
||||
return converter.convertId(id, targetType);
|
||||
}
|
||||
|
||||
@@ -876,8 +881,8 @@ public class QueryMapper {
|
||||
private Object applyFieldTargetTypeHintToValue(Field documentField, @Nullable Object value) {
|
||||
|
||||
if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()
|
||||
|| value instanceof Document || value instanceof DBObject || value instanceof Pattern
|
||||
|| value instanceof BsonRegularExpression) {
|
||||
|| value instanceof Document || value instanceof DBObject
|
||||
|| !SpecialTypeTreatment.INSTANCE.isConversionCandidate(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -1604,4 +1609,22 @@ public class QueryMapper {
|
||||
throw new IllegalStateException("No enclosing property source available");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Types that must not be converted
|
||||
*/
|
||||
enum SpecialTypeTreatment {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
private final Set<Class<?>> types = Set.of(Pattern.class, BsonRegularExpression.class);
|
||||
|
||||
boolean isConversionCandidate(@Nullable Object value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !types.contains(value.getClass());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1101,6 +1101,21 @@ public class QueryMapperUnitTests {
|
||||
assertThat(document.get("text")).isInstanceOf(BsonRegularExpression.class);
|
||||
}
|
||||
|
||||
@Test // GH-4674
|
||||
void shouldRetainRegexPatternForIdProperty() {
|
||||
|
||||
org.bson.Document javaRegex = mapper.getMappedObject(query(where("id").regex("^1234$")).getQueryObject(),
|
||||
context.getPersistentEntity(WithStringId.class));
|
||||
|
||||
assertThat(javaRegex.get("_id")).isInstanceOf(Pattern.class);
|
||||
|
||||
org.bson.Document bsonRegex = mapper.getMappedObject(
|
||||
query(where("id").regex(new BsonRegularExpression("^1234$"))).getQueryObject(),
|
||||
context.getPersistentEntity(WithStringId.class));
|
||||
|
||||
assertThat(bsonRegex.get("_id")).isInstanceOf(BsonRegularExpression.class);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2339
|
||||
void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user