Skip to content

fix: correct escape & anchor handling in likeToRegex/regexToLike#1174

Merged
velo merged 2 commits intoOpenFeign:masterfrom
renechoi:fix/like-regex-escape-anchors
Jun 9, 2025
Merged

fix: correct escape & anchor handling in likeToRegex/regexToLike#1174
velo merged 2 commits intoOpenFeign:masterfrom
renechoi:fix/like-regex-escape-anchors

Conversation

@renechoi
Copy link
Copy Markdown
Contributor

Problem Description

The original implementations of the methods likeToRegex and regexToLike in ExpressionUtils did not properly handle escaped characters (%, _, ., *, ?, etc.) and regex anchors (^ and $). This caused incorrect translations between SQL LIKE expressions and regex patterns, potentially leading to faulty queries and unexpected behavior during runtime.

Proposed Solution

To address these issues, the following modifications were made:

  1. Handling Escaped Characters:

    • Modified the loop logic within the likeToRegex method to correctly interpret escape sequences (e.g., \% and \_) in the LIKE pattern, ensuring they're preserved as literal characters in the resulting regex.
    • Updated the regexToLike method to accurately detect and preserve escaped special characters, thereby ensuring that regex patterns translate correctly back to SQL LIKE patterns.
  2. Correcting Regex Anchors (^ and $):

    • Adjusted the implementation in regexToLike to trim regex anchor characters (^ at the start and $ at the end) before conversion, allowing accurate translation into equivalent SQL LIKE patterns.

Testing

Comprehensive unit tests were written and added to ExpressionUtilsTest, verifying that:

  • Escape sequences are handled correctly (a\%b translates to regex ^a%b$ and vice versa).
  • Regex anchors are correctly managed and omitted in LIKE translations.
  • Existing conversion logic remains intact and performs correctly across various edge cases.

All tests pass successfully, ensuring the reliability and correctness of the revised methods.

Significance

These corrections significantly enhance the robustness and accuracy of Querydsl's pattern matching utilities. Developers relying on Querydsl for query construction will experience fewer surprises and less unexpected behavior due to properly managed escape sequences and regex anchors.

Practical Improvements in Usage

With these fixes:

  • Users can confidently use patterns containing escape characters and anchors, improving expressiveness and precision in queries.
  • Fewer workarounds and manual interventions will be required, reducing the likelihood of bugs in production systems.
  • Improved predictability and correctness make Querydsl easier to adopt and use effectively in enterprise and production environments.

renechoi and others added 2 commits May 22, 2025 23:05
- treat \% \_ as literals, escape regex metachars, strip outer ^/$ only
- add unit tests to guarantee accurate LIKE ↔ regex round-trips
Signed-off-by: Marvin Froeder <[email protected]>
@codecov
Copy link
Copy Markdown

codecov bot commented Jun 9, 2025

Codecov Report

Attention: Patch coverage is 73.91304% with 6 lines in your changes missing coverage. Please review.

Project coverage is 71.03%. Comparing base (028891d) to head (5f9ed66).
Report is 41 commits behind head on master.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
.../java/com/querydsl/core/types/ExpressionUtils.java 73.91% 1 Missing and 5 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1174      +/-   ##
============================================
+ Coverage     61.19%   71.03%   +9.84%     
+ Complexity       84       60      -24     
============================================
  Files           831      857      +26     
  Lines         32015    32367     +352     
  Branches       3590     3606      +16     
============================================
+ Hits          19590    22991    +3401     
+ Misses        11179     8149    -3030     
+ Partials       1246     1227      -19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@velo velo merged commit 86763dd into OpenFeign:master Jun 9, 2025
6 checks passed
velo added a commit that referenced this pull request Jun 9, 2025
* Add `TypeWrapperFactoryExpression` for Type-Safe Custom Number Mapping in Querydsl Aggregations (#1181)

* Introduce TypeWrapper to wrap Expression results into custom types

- Implement TypeWrapper<S, T> as a FactoryExpression
- Allows converting a source Expression (e.g. BigDecimal) to a domain type (e.g. Money)
- Prepares QueryDSL core to support custom aggregation projections without core API changes

* Add JPAQueryCustomTypeWrapperTest covering success and failure scenarios

- Verify IllegalArgumentException for unsupported custom types without wrapper
- Verify sum-and-wrap to Money via TypeWrapper in both direct and DTO projection use cases
- Ensure both positive and negative paths are exercised

* Fix: Improve handling of `contains()` on JPA collections mapped with `@Converter` to basic types (#1199)

* Fix: Early error for .contains() on @converter mapped JPA collections

* Test : add test code

* Add test cases to improve test coverage

* style: revert formatting change based on review feedback for consistency

* fix: correct escape & anchor handling in likeToRegex/regexToLike (#1174)

* fix: correct escape & anchor handling in likeToRegex/regexToLike
- treat \% \_ as literals, escape regex metachars, strip outer ^/$ only
- add unit tests to guarantee accurate LIKE ↔ regex round-trips

* Fix code format

Signed-off-by: Marvin Froeder <[email protected]>

---------

Signed-off-by: Marvin Froeder <[email protected]>
Co-authored-by: Marvin Froeder <[email protected]>

---------

Signed-off-by: Marvin Froeder <[email protected]>
Co-authored-by: 차동민 <[email protected]>
Co-authored-by: renechoi <[email protected]>
Co-authored-by: Marvin Froeder <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants