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

Loading of DependencyGroups fails with anonymous classes #16

Open
KevinBrowne opened this issue Feb 21, 2024 · 0 comments
Open

Loading of DependencyGroups fails with anonymous classes #16

KevinBrowne opened this issue Feb 21, 2024 · 0 comments
Labels

Comments

@KevinBrowne
Copy link
Member

KevinBrowne commented Feb 21, 2024

If multiple subclasses of DependencyGroup are defined and at least one of them is an anonymous class, Sinject::Container#load_groups will fail with an ArgumentError.

Steps to reproduce:

require 'sinject'
class MyDependenyGroup < Sinject::DependencyGroup
  def valid? = true
  def register(container) = container.register(key: :foo, class: String)
end
container = Sinject::Container.new(false)
container.load_groups
container.registered?(:foo)
# => true

Class.new(Sinject::DependencyGroup) do
  def valid? = true
  def register(container) = container.register(key: :bar, class: String)
end
container = Sinject::Container.new(false)
container.load_groups

Expected:

  • No error is raised

  • Both :foo and :bar are registered in the container

Actual:

  • The call to Sinject::Container#load_groups raises ArgumentError

  • No dependencies are registered.

This is caused by the container attempting to sort the DependencyGroup subclasses by name. If the class is anonymous its name, by definition, is nil and the comparison with nil raises. An anonymous DependencyGroup subclass will work if it is the only DependencyGroup.

@KevinBrowne KevinBrowne changed the title Loading of DependencyGroups fails with Anonymous classes Loading of DependencyGroups fails with anonymous classes Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant