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

help: Handling parentheses in the path pattern not related prefixes #76

Closed
whitlockjc opened this issue May 12, 2016 · 6 comments
Closed
Labels

Comments

@whitlockjc
Copy link

I currently use path-to-regexp in Sway and a user reported an issue that I believe will require me to do a little escaping prior to calling path-to-regexp. The user is currently trying to create an OData path like /Users(:id) but a request path of /Users(1) does not match the regex where I would expect it to. Now, I realize the reason for this is that parentheses are special but before I just go blindly escaping/hacking this to work, I wanted to get your opinion on how best to handle this situation.

Note: OpenAPI/Swagger use bracket-based variable naming in paths so in the linked issue you'll see /Users({id}) instead of /Users(:id). Do not let this trip you up because I already handle converting OpenAPI/Swagger paths to Express-style paths. Also, OpenAPI/Swagger paths do not allow regex in their path pattern (in case that matters with suggesting a solution).

@blakeembrey
Copy link
Member

Understood 😄 I actually wrote most of the RAML tooling for Node.js, so I'm also aware of this. Personally, I went and implemented my own path matcher and opened a PR to the Express.js router to make it extensible in 5.0. However, that's just an FYI since you asked for the best solution, and building a custom matcher would be best (but is not really available in Express.js today until, at least, router@2.0 is released). Would you be interested in using this approach? (I wouldn't mind spending some time exposing this work for you to re-use, I can also link to anything RAML-related which solved the same problems).

In terms of today, and if you want to continue mapping to current Express.js-compatible paths, you'll need to escape any special characters path-to-regexp has. Just be aware that although the current path-to-regexp is 1.x and has a lot of improved features, current Express.js uses 0.1.x and has a few quirks when it comes to matching.

@whitlockjc
Copy link
Author

Is there a list of special characters? I am sure I can figure it out by code grokking but if you already know, or want to mention it for posterity.

@blakeembrey
Copy link
Member

blakeembrey commented May 13, 2016

Sure. Of the top of my head, I believe it's (, ), :, *, + and ?.

Reference: https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L27

@whitlockjc
Copy link
Author

Thanks @blakeembrey. As for my being interested in you breaking out the reusable stuff from your RAML work, I'd love to help out. I'm all for helping the community.

@blakeembrey
Copy link
Member

@whitlockjc Most of the work come from Osprey (https://github.com/mulesoft/osprey) which mostly involved a bit of refactoring of the router (pillarjs/router#29) to support custom path handlers, at which point I wrote https://github.com/mulesoft-labs/raml-path-match. I've been meaning to correct that router and release it separately so more people can actually use it, but the PR's been around for almost a year and a half now

/cc @dougwilson

@blakeembrey
Copy link
Member

Closing this since the issue here was answered. You can escape path-to-regexp special characters with \\.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants