-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
Criteria.regex
converted to String
for @Field(targetType = FieldType.STRING)
property
…eryMapper`. QueryMapper no longer attempts to convert regex objects when a field has an explicit write target. Closes #4649
…eryMapper`. QueryMapper no longer attempts to convert regex objects when a field has an explicit write target. Closes #4649
Hi, this problem is still happening with ID fields. In |
Thanks for your response @sunxiang0918, this fix was already released: 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 |
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 convertjava.util.regex.Pattern
tojava.lang.String
, which prevents it from entering the mongo-driver'sPatternCodec
. 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 theObjectToStringConverter
. In theQueryMapper#applyFieldTargetTypeHintToValue
method of spring-data-mongo, the conversionService'scanConvert(...)
method is called to determine if a Pattern can be converted to a String. In previous versions, without this converter,canConvert(...)
would returnfalse
, indicating that conversion was not possible and thus returningjava.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 convertjava.util.regex.Pattern
to String, leading to the aforementioned bug.spring-core:6.0.11 :

spring-core:6.1.3:

spring-core:6.1.0+:

The text was updated successfully, but these errors were encountered: