Skip to content

Criteria.regex converted to String for @Field(targetType = FieldType.STRING) property #4649

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
sunxiang0918 opened this issue Feb 27, 2024 · 3 comments
Assignees
Labels
type: regression A regression from a previous release

Comments

@sunxiang0918
Copy link

sunxiang0918 commented Feb 27, 2024

When I use the Criteria class to construct a query with a regex(using mongoTemplate.find() ), the query does not return a result using version 4.2.0+, whereas the same code, using version 4.1.0, returns the result correctly.

Example: criteria.regex("^abc").

I followed the source code to try to find the cause of the problem. Here's what I found:

When querying in mongoTemplate's doFind method, the query conditions passed in are transformed as follows: Document mappedQuery = queryContext.getMappedQuery(entity);. In versions of spring-data-mongo 4.2+ (spring-core 6.1.0+), the transformed mappedQuery will convert java.util.regex.Pattern to java.lang.String, which prevents it from entering the mongo-driver's PatternCodec. Consequently, the resulting mongo query condition changes from { "$regex": "^abc" } to "^abc", meaning it no longer uses "regex" for regex queries but instead becomes an "equal" comparison query.

The root cause of this issue lies in the fact that in spring-core 6.1.0+ versions, a PR was merged (spring-core PR-24311) that added a pattern-to-string converter to DefaultConversionService in spring-core, namely the ObjectToStringConverter. In the QueryMapper#applyFieldTargetTypeHintToValue method of spring-data-mongo, the conversionService's canConvert(...) method is called to determine if a Pattern can be converted to a String. In previous versions, without this converter, canConvert(...) would return false, indicating that conversion was not possible and thus returning java.util.regex.Pattern as is.

However, in spring-core 6.1.0+ versions, with the presence of this converter, canConvert(...) now returns true, indicating conversion is possible. As a result, ObjectToStringConverter is invoked to convert java.util.regex.Pattern to String, leading to the aforementioned bug.

spring-core:6.0.11 :
spring_core_6 0

spring-core:6.1.3:
spring_core_6 1

spring-core:6.1.0+:
image

image
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 27, 2024
@mp911de mp911de changed the title After upgrading spring-data-mongo from 4.1.2 to 4.2.2, the regex query of mongoTemplate doesn't return results. Criteria.regex converted to String for @Field(targetType = FieldType.STRING) property Feb 27, 2024
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 27, 2024
@mp911de mp911de self-assigned this Feb 27, 2024
@mp911de mp911de added type: regression A regression from a previous release and removed type: bug A general bug labels Feb 27, 2024
mp911de added a commit that referenced this issue Feb 27, 2024
…eryMapper`.

QueryMapper no longer attempts to convert regex objects when a field has an explicit write target.

Closes #4649
mp911de added a commit that referenced this issue Feb 27, 2024
…eryMapper`.

QueryMapper no longer attempts to convert regex objects when a field has an explicit write target.

Closes #4649
@spekdrum
Copy link

spekdrum commented Mar 26, 2024

Hi, this problem is still happening with ID fields. In QueryMapper#getMappedValue method, applyFiledTargetTypeHitToValue() returns the Pattern object correctly after the fix, but later ends calling QueryMapper#convertId() method, which ends calling getConversionService().canConvert() in MongoConverter.convertId() method. This is returning true, as described by the op, so Pattern is converted to String anyway.

image

image

@sunxiang0918
Copy link
Author

No, I'm still waiting for the [4.2.4] version Release.
In my program, I temporarily customized the MongoConverter and deleted the pattern-to-string converter that I don't currently use.
I will upgrade after this problem is officially fixed.

image

@spekdrum
Copy link

spekdrum commented Mar 26, 2024

Thanks for your response @sunxiang0918, this fix was already released:
https://github.com/spring-projects/spring-data-mongodb/releases/tag/4.2.4

I edited my post because I found this happens with ID fields and the logic treats them specifically, maybe with other kind of fields is fixed.

I better open a new issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

4 participants