Skip to content
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

RegEx using greedy qualifier for N times is quoted incorrectly #676

Open
ruir6o opened this issue Nov 18, 2021 · 3 comments
Open

RegEx using greedy qualifier for N times is quoted incorrectly #676

ruir6o opened this issue Nov 18, 2021 · 3 comments

Comments

@ruir6o
Copy link

ruir6o commented Nov 18, 2021

implementation 'org.http4k:http4k-core:4.17.0.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.6.0'

I have just found out about http4k and I decided to create a simple service with it. My very first attempt at getting a path parameter to be validated against a UUID RegEx is failing however. This is how I've defined the RegEx:

const val COMPACT_UUID_REGEX = "[a-fA-F0-9]{32}"
const val PRETTY_UUID_REGEX = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"
const val UUID_REGEX = "(?:${COMPACT_UUID_REGEX}|${PRETTY_UUID_REGEX})"

And it is put in a router by using

routes(
    "api/v1/sample/{id:$UUID_REGEX}" bind POST to { /* ... */}
)

However running the application yields:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed counted closure near index 35
\Qapi/v1/sample/\E((?:[a-fA-F0-9]{32)\Q|[a-fA-F0-9]\E([^/]+)\Q-[a-fA-F0-9]\E([^/]+)\Q-[a-fA-F0-9]\E([^/]+)\Q-[a-fA-F0-9]\E([^/]+)\Q-[a-fA-F0-9]\E([^/]+)\Q)}\E

Looking at the exception it seems like http4k is replacing the { and } in an attempt to quote literal path segments, however it is applying it to the regex section as well, interfering with the greedy qualifiers defined in the UUID RegEx.

@ruir6o ruir6o changed the title Regex using greedy qualifier for N times is quoted incorrectly RegEx using greedy qualifier for N times is quoted incorrectly Nov 18, 2021
@daviddenton
Copy link
Member

Thanks for the report. It looks like a tricky one to solve TBH (Regexes 🙄 ). I've added a failing test anyway here

@ruir6o
Copy link
Author

ruir6o commented Nov 26, 2021

@daviddenton I think a simple custom parser could do the trick. I've created a simple PR (#678) to showcase this.

ruir6o added a commit to ruir6o/http4k that referenced this issue Nov 28, 2021
Replaces the UriTemplate logic by a custom parser that handles complex
reguler expressions (i.e. regular expressions that contain the
delimiters for the path parameters).

Issue-Id: http4k#676
ruir6o added a commit to ruir6o/http4k that referenced this issue Nov 28, 2021
Replaces the UriTemplate logic by a custom parser that handles complex
regular expressions (i.e. regular expressions that contain the
delimiters for the path parameters).

Issue-Id: http4k#676
ruir6o added a commit to ruir6o/http4k that referenced this issue Nov 28, 2021
Replaces the UriTemplate logic by a custom parser that handles complex
regular expressions (i.e. regular expressions that contain the
delimiters for the path parameters).

Issue-Id: http4k#676
@ruir6o ruir6o mentioned this issue Nov 28, 2021
@Angel-Ponce
Copy link

Angel-Ponce commented May 9, 2024

Same problem, there are some solution to fix it?

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

No branches or pull requests

3 participants