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
pull/4721/head
Christoph Strobl 2 years ago committed by Mark Paluch
parent
commit
03de6f0e5a
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

@ -815,6 +815,11 @@ public class QueryMapper {
*/ */
@Nullable @Nullable
public Object convertId(@Nullable Object id, Class<?> targetType) { public Object convertId(@Nullable Object id, Class<?> targetType) {
if (!SpecialTypeTreatment.INSTANCE.isConversionCandidate(id)) {
return id;
}
return converter.convertId(id, targetType); return converter.convertId(id, targetType);
} }
@ -876,8 +881,8 @@ public class QueryMapper {
private Object applyFieldTargetTypeHintToValue(Field documentField, @Nullable Object value) { private Object applyFieldTargetTypeHintToValue(Field documentField, @Nullable Object value) {
if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget() if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()
|| value instanceof Document || value instanceof DBObject || value instanceof Pattern || value instanceof Document || value instanceof DBObject
|| value instanceof BsonRegularExpression) { || !SpecialTypeTreatment.INSTANCE.isConversionCandidate(value)) {
return value; return value;
} }
@ -1604,4 +1609,22 @@ public class QueryMapper {
throw new IllegalStateException("No enclosing property source available"); 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());
}
}
} }

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

@ -1101,6 +1101,21 @@ public class QueryMapperUnitTests {
assertThat(document.get("text")).isInstanceOf(BsonRegularExpression.class); 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 @Test // DATAMONGO-2339
void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly() { void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly() {

Loading…
Cancel
Save