Browse Source

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
issue/4755
Christoph Strobl 2 years ago committed by Mark Paluch
parent
commit
fd13c12817
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 27
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 15
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

27
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

@ -788,6 +788,11 @@ public class QueryMapper { @@ -788,6 +788,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);
}
@ -849,8 +854,8 @@ public class QueryMapper { @@ -849,8 +854,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;
}
@ -1569,4 +1574,22 @@ public class QueryMapper { @@ -1569,4 +1574,22 @@ public class QueryMapper {
public MongoConverter getConverter() {
return converter;
}
/*
* 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());
}
}
}

15
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

@ -1026,6 +1026,21 @@ public class QueryMapperUnitTests { @@ -1026,6 +1026,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() {

Loading…
Cancel
Save