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

Ensure routes are unique #1538

Merged
merged 4 commits into from
Jul 12, 2021
Merged

Ensure routes are unique #1538

merged 4 commits into from
Jul 12, 2021

Conversation

paulcsmith
Copy link
Member

@paulcsmith paulcsmith commented Jul 10, 2021

Tested by changing one of our routes in the specs to collide and it worked

Closes #1539

Screen Shot 2021-07-10 at 4 58 35 PM

@paulcsmith paulcsmith marked this pull request as ready for review July 10, 2021 20:59
@paulcsmith paulcsmith force-pushed the pcs/enforce-unique branch from 9d22089 to 7fe8cfa Compare July 10, 2021 21:00
Comment on lines +154 to +155
# So "/users/:user_id" is changed to "/users/:normalized"
{% normalized_path = original_path.gsub(/(\:\w*)/, ":normalized") %}
Copy link
Member Author

@paulcsmith paulcsmith Jul 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way /users/:id and /users/:user_id are counted as the same route, since that's how the router tries to match them. I tested this locally to make sure it works

@paulcsmith
Copy link
Member Author

There are some edge cases with optional route parts, but overall this should catch most route collissions

@@ -0,0 +1,6 @@
# Include this in an action to skip route uniqueness checks.
module Lucky::SkipUniqueRouteCheck
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that it's necessary to provide this. If you skip the compile time check, you're still going to get a runtime error from the duplicate checks in lucky_router https://github.com/luckyframework/lucky_router/blob/d6020f0c404e033abb920cf29e34ec8d752525c2/src/lucky_router/matcher.cr#L50

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. We discussed this in chat and I think we can leave this here pre-1.0 (or at least for one version) since this uses different logic for checking duplicates. If it seems to not have issues then we can remove it!

Does that sound good?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue to track this here: #1541

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! This is a great addition.

# So "/users/:user_id" is changed to "/users/:normalized"
{% normalized_path = original_path.gsub(/(\:\w*)/, ":normalized") %}

{% if already_used_route = NORMALIZED_ROUTES.find { |route| route[:normalized_path] == normalized_path && route[:method] == method } %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this matter or not, but would it make sense (from a lookup perspective) to make NORMALIZED_ROUTES a Hash instead? The normalized path would be the key in this case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinsjoberg I tried, but there are some issues with Hash/NameTuples in macro constants that I couldn't seem to get around :(

Also since it needs to match both method and normalized path I think this may actually work better. Since if we used the normalized path as key that would mean there is no way to lookup based on method. I suppose we could combine the method and path into one string key though 🤔 Maybe someone can do a cleanup pass later and can figure out how to use the Hash in a macro

@paulcsmith paulcsmith merged commit 7d91f1f into master Jul 12, 2021
@paulcsmith paulcsmith deleted the pcs/enforce-unique branch July 12, 2021 17:55
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.

Check for route collisions at compile-time
4 participants