Skip to content

Append trailing slash when the path is generated #43287

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

Merged
merged 1 commit into from
Sep 24, 2021

Conversation

casperisfine
Copy link
Contributor

@casperisfine casperisfine commented Sep 22, 2021

Fix: #43219

Instead of trying to parse the path later to insert the slash or bail out if the path look like it has a format indicator.

This patch is based on the assumption that url_for(path: '/blah') is not an actual public API. Which isn't something I'm certain of. But at the very least it doesn't seem documented.

Git History

The trailing_slash option was added in db4f421. However it was tacked on as a string operation on the return value of the URL Generator.

I don't understand why it wasn't added as an option of the generator itself.

The consequence is that then URL then have to be parsed to know where to insert the slash and wether it is safe or not to do.

#8704 fixed a few corner cases and added the following two tests:

    assert_equal 'http://www.example.com/books/?q=code', url_for(trailing_slash: true, path: '/books?q=code')
    assert_equal 'http://www.example.com/books/?spareslashes=////', url_for(trailing_slash: true, path: '/books?spareslashes=////')

But as far as I can tell they don't correspond to any real behavior. Even if you use url_for with the implicit current request arguments as to normalize the current URL, the path: option is not passed and the URL generated again from :controller and action.

@silva96
Copy link
Contributor

silva96 commented Sep 22, 2021

@casperisfine there's a small rubocop offense

@@ -42,9 +42,6 @@ class RequestUrlFor < BaseRequestTest

assert_equal "/books", url_for(only_path: true, path: "/books")

assert_equal "http://www.example.com/books/?q=code", url_for(trailing_slash: true, path: "/books?q=code")
Copy link
Member

Choose a reason for hiding this comment

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

Why were those tests removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I believe they test something that's the underlying implementation an not public API #8704

Assuming that url_for(path: "/foo") isn't public API (it isn't documented as far as I can tell).

Copy link
Member

@rafaelfranca rafaelfranca Sep 22, 2021

Choose a reason for hiding this comment

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

But why not delete the test above that as well? I was not sure if it was because the behavior changed and the test failed, or just because we were cleaning up implementation detail tests.

In general we test non public API in the rails test suite if it is necessary, but if it is being tested using the public API it is fine to remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But why not delete the test above that as well? I was not sure if it was because the behavior changed and the test failed

Right. These two failed because they were testing the feature through a private API.

Copy link
Member

Choose a reason for hiding this comment

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

So there is a change in behavior? I find that concerning. Would this change reintroduce the behavior #8704 was supposed to fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I don't think so. But I feel like we're talking past each others, it's my fault I didn't take the time to properly braindump in the PR description, I'll do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rafaelfranca I added some more context to the PR and also added some more integration style tests.

Instead of trying to parse the path later to insert
the slash or bail out if the path look like it has a
format indicator.

This patch is based on the assumption that `url_for(path: '/blah')`
is not an actual public API.
@byroot byroot merged commit 581aac7 into rails:main Sep 24, 2021
@@ -71,7 +71,8 @@ def path_for(options)
path = options[:script_name].to_s.chomp("/")
path << options[:path] if options.key?(:path)

add_trailing_slash(path) if options[:trailing_slash]
path = "/" if options[:trailing_slash] && path.blank?
Copy link
Member

Choose a reason for hiding this comment

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

FYI #44373

casperisfine pushed a commit to Shopify/rails that referenced this pull request Feb 15, 2022
Ref: rails#43287
Fix: rails#44373

The `OptimizedUrlHelper` wasn't considering this option.
byroot added a commit that referenced this pull request Feb 15, 2022
Ref: #43287
Fix: #44373

The `OptimizedUrlHelper` wasn't considering this option.
public-rant pushed a commit to opensource-rant/rails that referenced this pull request Feb 17, 2022
Ref: rails#43287
Fix: rails#44373

The `OptimizedUrlHelper` wasn't considering this option.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Trailing slashes are not added when url has a dot
5 participants