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

Sinatra::Namespace nesting similarly-named namespaces #1251

Closed
mwpastore opened this issue Feb 6, 2017 · 5 comments
Closed

Sinatra::Namespace nesting similarly-named namespaces #1251

mwpastore opened this issue Feb 6, 2017 · 5 comments

Comments

@mwpastore
Copy link
Member

mwpastore commented Feb 6, 2017

Still trying to wrap my brain around this but wanted to get an issue going for feedback, particularly from @zzak since he rewrote Sinatra::Namespace for 2.0 and is hopefully familiar with its inner-workings. 😄

Take the following sample application:

require 'sinatra/base'
require 'sinatra/namespace'

class MyApp < Sinatra::Base
  register Sinatra::Namespace

  helpers do
    def dump_args(*args)
      args.inspect
    end
  end

  namespace '/foo' do
    helpers do
      def dump_args(*args)
        super(:foo, *args)
      end
    end

    get '' do
      "#{dump_args}\n"
    end
  end

  namespace '/foo-bar' do
    helpers do
      def dump_args(*args)
        super(:foo_bar, *args)
      end
    end

    get '' do
      "#{dump_args}\n"
    end
  end

  run! if $0 == __FILE__
end

Expected output:

$ curl localhost:4567/foo
[:foo]
$ curl localhost:4567/foo-bar
[:foo_bar]

Summary: The super of each namespace's dump_args helper is expected to be the dump_args helper defined in the parent application.

Actual output:

$ curl localhost:4567/foo
[:foo]
$ curl localhost:4567/foo-bar
[:foo, :foo_bar]

Summary: The super of the second namespace's dump_args helper is the dump_args helper defined in the first namespace.

Although the two routes are in two separate namespaces, and hence, ostensibly, in two separate modules, it appears as though the latter is nested under the former. This only happens when the longer route starts with the same string as the shorter route; i.e. if the second namespace were named simply /bar, this issue would not manifest.

I can confirm this issue is present in Sinatra 1.4.8 (with and without Mustermann), 2.0.0.beta2, and master tip.

@mwpastore
Copy link
Member Author

mwpastore commented Feb 6, 2017

Here is a workaround for Sinatra 2.0+:

  namespace %r{/foo(?![^/])} do

Or:

  namespace %r{/foo\b} do

Unfortunately, it breaks nested namespaces under Sinatra 1.4.8 (even, weirdly, with Mustermann), so I'll need something better for my own purposes.

@zzak
Copy link
Member

zzak commented Feb 7, 2017

@mwpastore Are you seeing this without Mustermann?

@mwpastore
Copy link
Member Author

@zzak With and without Mustermann on 1.4.8.

@zzak
Copy link
Member

zzak commented Feb 7, 2017

Well, that sucks.. but if the behavior is the same between 1.x and 2.x then we can at least mark it as a known issue.

@namusyaka
Copy link
Member

@mwpastore Could you confirm #1253?

mwpastore added a commit to mwpastore/sinja that referenced this issue Feb 16, 2017
@zzak zzak added the bug label Mar 4, 2017
@zzak zzak added this to the 2.0.0 milestone Mar 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants