Skip to content

Fix conversion of regular expression targeting id properties in queries #4718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4674-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4674-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4674-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4674-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,11 @@ public Object convertId(@Nullable Object id) {
*/
@Nullable
public Object convertId(@Nullable Object id, Class<?> targetType) {

if (!SpecialTypeTreatment.INSTANCE.isConversionCandidate(id)) {
return id;
}

return converter.convertId(id, targetType);
}

Expand Down Expand Up @@ -876,8 +881,8 @@ protected boolean isKeyword(String candidate) {
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;
}

Expand Down Expand Up @@ -1604,4 +1609,22 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,21 @@ void shouldRetainRegexPattern() {
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() {

Expand Down
Loading