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

Path segments in base_url can get clobbered by UrlHelpers #249

Closed
alassek opened this issue Jan 30, 2023 · 0 comments · Fixed by #250
Closed

Path segments in base_url can get clobbered by UrlHelpers #249

alassek opened this issue Jan 30, 2023 · 0 comments · Fixed by #250
Assignees
Labels
Milestone

Comments

@alassek
Copy link
Contributor

alassek commented Jan 30, 2023

Expected Result

Configuring a base_url with a path prefix should preserve it when generating URLs

base_url = URI("http://example.com/hanami")
router = Hanami::Router.new(base_url: base_url) do
  get "/test", to: ->(env) { [200, {}, ["OK"]] }, as: :test
end
router.url(:test) # => #<URI::HTTP http://example.com/hanami/test>

Actual Result

When base_url is a URI object, the path prefix gets replaced by the route path.

base_url = URI("http://example.com/hanami")
router = Hanami::Router.new(base_url: base_url) do
  get "/test", to: ->(env) { [200, {}, ["OK"]] }, as: :test
end
router.url(:test) # => #<URI::HTTP http://example.com/test>

Further Context

There is a type mismatch in how base_url is being configured. In this project, it is being tested with a string

let(:router) do
e = endpoint
Hanami::Router.new(base_url: base_url) do
get "/hanami", to: e, as: :fixed
get "/flowers/:id", to: e, as: :variables
get "/books/:id", id: /\d+/, to: e, as: :constraints
get "/articles(.:format)", to: e, as: :optional
get "/files/*glob", to: e, as: :glob
end
end
let(:endpoint) { ->(*) { [200, {}, ["Hi!"]] } }
let(:base_url) { "https://hanami.test" }

However, in the framework proper the base_url configuration is being coerced to a URI

https://github.com/hanami/hanami/blob/6780d4ea87b54ce543c8aa18fed0e46b8fbf84d6/lib/hanami/config.rb#L115-L128

The UrlHelpers class constructs a URL combining base_url with the generated path using +

# @since 2.0.0
# @api private
def url(name, variables = {})
@base_url + path(name, variables)
end

When base_url is a string, this is a simple string concatenation. When base_url is a URI, + is an alias for merge which interprets a path beginning with / as absolute.

alassek added a commit to alassek/hanami-router that referenced this issue Jan 30, 2023
There are subtle differences in behavior depending on whether it is a
String or a URI. This patch forces it to be a URI and uses Prefix to
construct absolute URLs.

Fixes hanami#249
alassek added a commit to alassek/hanami-router that referenced this issue Jan 30, 2023
There are subtle differences in behavior depending on whether it is a
String or a URI. This patch forces it to be a URI and uses Prefix to
construct absolute URLs.

Fixes hanami#249
@jodosha jodosha self-assigned this Feb 9, 2023
@jodosha jodosha added the bug label Feb 9, 2023
@jodosha jodosha added this to the v2.0.3 milestone Feb 9, 2023
alassek added a commit to alassek/hanami-router that referenced this issue Feb 10, 2023
There are subtle differences in behavior depending on whether it is a
String or a URI. This patch forces it to be a URI and uses Prefix to
construct absolute URLs.

Fixes hanami#249
jodosha pushed a commit that referenced this issue Feb 10, 2023
There are subtle differences in behavior depending on whether it is a
String or a URI. This patch forces it to be a URI and uses Prefix to
construct absolute URLs.

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

Successfully merging a pull request may close this issue.

2 participants