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

Reenable routes with optional portions #275

Open
timriley opened this issue Dec 4, 2024 · 1 comment
Open

Reenable routes with optional portions #275

timriley opened this issue Dec 4, 2024 · 1 comment

Comments

@timriley
Copy link
Member

timriley commented Dec 4, 2024

All the following examples from spec/integration/hanami/router/generation_spec.rb are skipped.

I think these were most likely disabled when we first introduced the trie structure for 2.0, and then we did not reintroduce this behaviour afterwards.

I think this is useful to have. We should have a look to see whether we can achieve it again.

context "variable with optional format" do
  let(:router) do
    described_class.new do
      get "/:var(.:format)", as: :a, to: -> {}
    end
  end

  it "generates relative and absolute URLs" do
    runner.run!([
      [:a, "/test.html", {var: "test", format: "html"}],
      [:a, "/test", {var: "test"}]
    ])
  end
end

context "variable with optional variable" do
  let(:router) do
    described_class.new do
      get "/:var1(/:var2)", as: :a, to: -> {}
    end
  end

  xit "generates relative and absolute URLs" do
    runner.run!([
      [:a, "/foo/bar", {var1: "foo", var2: "bar"}],
      [:a, "/foo", {var1: "foo"}]
    ])
  end
end

context "variable with optional variable and format" do
  let(:router) do
    described_class.new do
      get "/:var1(/:var2.:format)", as: :a, to: -> {}
    end
  end

  xit "generates relative and absolute URLs" do
    runner.run!([
      [:a, "/test/test2.html", {var1: "test", var2: "test2", format: "html"}]
    ])
  end
end

context "variable with optional nested variables" do
  let(:router) do
    described_class.new do
      get "/:var1(/:var2(/:var3))", as: :a, to: -> {}
    end
  end

  xit "generates relative and absolute URLs" do
    runner.run!([
      [:a, "/var/fooz/baz", {var1: "var", var2: "fooz", var3: "baz"}],
      [:a, "/var/fooz", {var1: "var", var2: "fooz"}],
      [:a, "/var", {var1: "var"}]
    ])
  end
end
@dcr8898
Copy link
Contributor

dcr8898 commented Jan 6, 2025

There is no reason this can't be done with the current trie implementation. It would just require parsing the route specification containing optional segments, and then adding all of the possible route permutations to the trie.

If optional segments are used a lot, especially multiple optional segments in the same route, the proliferation of routes could lead to slower start times (see #278 for related issue).

I would be happy to work on this when I have a little more time.

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

No branches or pull requests

2 participants