router icon indicating copy to clipboard operation
router copied to clipboard

Using mount adds trailing slash

Open ahx opened this issue 5 months ago • 4 comments

This is the code that I am using to debug this:

# config.ru
Router = Hanami::Router.new do
  mount ->(env) { binding.irb }, at: '/settings'
end

run Router

When making a request GET /settings the rack env includes these values:

"SCRIPT_NAME" => "/settings",
"PATH_INFO" => "/"

Put together this ends up with the request path "/settings/" instead of "/settings" as in the original request.

For me this is a problem, because I am trying to map the request to a path in an OpenAPI definition and that one just knows about "/settings" not "/settings/". Also this was quite surprising to see that there seems to be no way to get to the original request path (without trailing slash in this case) when using hanami-router's mount ~~or scope~~ (not sure about scope).

To me this looks like a bug. Any thoughts?

ahx avatar Nov 25 '25 23:11 ahx

Since you asked for thoughts, not solutiions:

Coming from HTTP semantics, I'd say Hanami is correct. Having not trailing slash indicates requesting a file from a folder (like /srv/www/settings), whereas the trailing slash indicates requesting the index or index file of a folder (/srv/www/settings/index.html). Assuming you also have URL paths like /settings/details or /settings/details/, this somewhat invalidates the hypothetical presence of a settings file.

Of course, these whole semantics are from a time long since gone, so not sure how much it should be applied to the world of HTTP API services.

Fruetel avatar Nov 26 '25 07:11 Fruetel

Thanks for this report! I need a bit of time to dig in and understand this properly before getting you a reply. I'll get back to you soon.

timriley avatar Nov 26 '25 22:11 timriley

@ahx Thanks again for your fantastic issue reports :)

I've come up with a fix for this in #295, which also does a bit of extra work to ensure compatibility with root routes in mounted routers. Would you mind taking a look and letting me know if that'd still solve it for you?

timriley avatar Dec 12 '25 06:12 timriley

Hey Tim, https://github.com/hanami/hanami-router/pull/295 just works as expected. Together with your fix for https://github.com/hanami/hanami-router/issues/291 this makes hanami-router for my use case and probably other use cases outside of hanami. Thanks for your work on this.

ahx avatar Dec 14 '25 21:12 ahx

@ahx I've just released the fixes for this and #290 in hanami-router v2.3.1 :)

timriley avatar Dec 17 '25 10:12 timriley