Skip to content

Commit fd13c12

Browse files
christophstroblmp911de
authored andcommitted
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
1 parent 99171f3 commit fd13c12

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

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

+25-2
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,11 @@ public Object convertId(@Nullable Object id) {
788788
*/
789789
@Nullable
790790
public Object convertId(@Nullable Object id, Class<?> targetType) {
791+
792+
if (!SpecialTypeTreatment.INSTANCE.isConversionCandidate(id)) {
793+
return id;
794+
}
795+
791796
return converter.convertId(id, targetType);
792797
}
793798

@@ -849,8 +854,8 @@ protected boolean isKeyword(String candidate) {
849854
private Object applyFieldTargetTypeHintToValue(Field documentField, @Nullable Object value) {
850855

851856
if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()
852-
|| value instanceof Document || value instanceof DBObject || value instanceof Pattern
853-
|| value instanceof BsonRegularExpression) {
857+
|| value instanceof Document || value instanceof DBObject
858+
|| !SpecialTypeTreatment.INSTANCE.isConversionCandidate(value)) {
854859
return value;
855860
}
856861

@@ -1569,4 +1574,22 @@ public MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentPropert
15691574
public MongoConverter getConverter() {
15701575
return converter;
15711576
}
1577+
1578+
/*
1579+
* Types that must not be converted
1580+
*/
1581+
enum SpecialTypeTreatment {
1582+
1583+
INSTANCE;
1584+
1585+
private final Set<Class<?>> types = Set.of(Pattern.class, BsonRegularExpression.class);
1586+
1587+
boolean isConversionCandidate(@Nullable Object value) {
1588+
if (value == null) {
1589+
return false;
1590+
}
1591+
1592+
return !types.contains(value.getClass());
1593+
}
1594+
}
15721595
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,21 @@ void shouldRetainRegexPattern() {
10261026
assertThat(document.get("text")).isInstanceOf(BsonRegularExpression.class);
10271027
}
10281028

1029+
@Test // GH-4674
1030+
void shouldRetainRegexPatternForIdProperty() {
1031+
1032+
org.bson.Document javaRegex = mapper.getMappedObject(query(where("id").regex("^1234$")).getQueryObject(),
1033+
context.getPersistentEntity(WithStringId.class));
1034+
1035+
assertThat(javaRegex.get("_id")).isInstanceOf(Pattern.class);
1036+
1037+
org.bson.Document bsonRegex = mapper.getMappedObject(
1038+
query(where("id").regex(new BsonRegularExpression("^1234$"))).getQueryObject(),
1039+
context.getPersistentEntity(WithStringId.class));
1040+
1041+
assertThat(bsonRegex.get("_id")).isInstanceOf(BsonRegularExpression.class);
1042+
}
1043+
10291044
@Test // DATAMONGO-2339
10301045
void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly() {
10311046

0 commit comments

Comments
 (0)