Skip to content

Commit 72f19c6

Browse files
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.
1 parent 1106a5a commit 72f19c6

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,11 @@ public Object convertId(@Nullable Object id) {
815815
*/
816816
@Nullable
817817
public Object convertId(@Nullable Object id, Class<?> targetType) {
818+
819+
if (!SpecialTypeTreatment.INSTANCE.isConversionCandidate(id)) {
820+
return id;
821+
}
822+
818823
return converter.convertId(id, targetType);
819824
}
820825

@@ -876,8 +881,8 @@ protected boolean isKeyword(String candidate) {
876881
private Object applyFieldTargetTypeHintToValue(Field documentField, @Nullable Object value) {
877882

878883
if (value == null || documentField.getProperty() == null || !documentField.getProperty().hasExplicitWriteTarget()
879-
|| value instanceof Document || value instanceof DBObject || value instanceof Pattern
880-
|| value instanceof BsonRegularExpression) {
884+
|| value instanceof Document || value instanceof DBObject
885+
|| !SpecialTypeTreatment.INSTANCE.isConversionCandidate(value)) {
881886
return value;
882887
}
883888

@@ -1604,4 +1609,22 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
16041609
throw new IllegalStateException("No enclosing property source available");
16051610
}
16061611
}
1612+
1613+
/*
1614+
* Types that must not be converted
1615+
*/
1616+
enum SpecialTypeTreatment {
1617+
1618+
INSTANCE;
1619+
1620+
private final Set<Class<?>> types = Set.of(Pattern.class, BsonRegularExpression.class);
1621+
1622+
boolean isConversionCandidate(@Nullable Object value) {
1623+
if (value == null) {
1624+
return false;
1625+
}
1626+
1627+
return !types.contains(value.getClass());
1628+
}
1629+
}
16071630
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,21 @@ void shouldRetainRegexPattern() {
11011101
assertThat(document.get("text")).isInstanceOf(BsonRegularExpression.class);
11021102
}
11031103

1104+
@Test // GH-4674
1105+
void shouldRetainRegexPatternForIdProperty() {
1106+
1107+
org.bson.Document javaRegex = mapper.getMappedObject(query(where("id").regex("^1234$")).getQueryObject(),
1108+
context.getPersistentEntity(WithStringId.class));
1109+
1110+
assertThat(javaRegex.get("_id")).isInstanceOf(Pattern.class);
1111+
1112+
org.bson.Document bsonRegex = mapper.getMappedObject(
1113+
query(where("id").regex(new BsonRegularExpression("^1234$"))).getQueryObject(),
1114+
context.getPersistentEntity(WithStringId.class));
1115+
1116+
assertThat(bsonRegex.get("_id")).isInstanceOf(BsonRegularExpression.class);
1117+
}
1118+
11041119
@Test // DATAMONGO-2339
11051120
void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly() {
11061121

0 commit comments

Comments
 (0)