From 8d8f9a90b253b040503316e5a4d0e6558bc28c5d Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 3 Mar 2022 11:42:06 +1100 Subject: [PATCH] Only pass-through kwargs if super class can take them --- lib/dry/auto_inject/strategies/kwargs.rb | 2 +- spec/dry/auto_inject_spec.rb | 33 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/dry/auto_inject/strategies/kwargs.rb b/lib/dry/auto_inject/strategies/kwargs.rb index 1c53f9f..c2781cf 100644 --- a/lib/dry/auto_inject/strategies/kwargs.rb +++ b/lib/dry/auto_inject/strategies/kwargs.rb @@ -72,7 +72,7 @@ def define_initialize_with_splat(super_parameters) else super_kwargs = slice_kwargs.(kwargs, super_parameters) - if super_kwargs.any? + if super_kwargs.any? && super_parameters.keyword_names.any? super(*args, **super_kwargs, &block) else super(*args, &block) diff --git a/spec/dry/auto_inject_spec.rb b/spec/dry/auto_inject_spec.rb index 87afdfe..9ca068a 100644 --- a/spec/dry/auto_inject_spec.rb +++ b/spec/dry/auto_inject_spec.rb @@ -557,6 +557,39 @@ def initialize(other) expect(instance.three).to eq 3 end end + + context "autoinject in class and parent with regular argument in super class" do + let(:super_klass) do + Class.new do + attr_reader :other + + def initialize(other) + @other = other + end + end + end + + let(:parent_klass) do + Class.new(super_klass) do + include Test::AutoInject.kwargs[:one, :two] + end + end + + let(:child_class) do + Class.new(parent_klass) do + include Test::AutoInject.kwargs["namespace.three"] + end + end + + it "works" do + instance = child_class.new(:other) + + expect(instance.other).to eq :other + expect(instance.one).to eq 1 + expect(instance.two).to eq 2 + expect(instance.three).to eq 3 + end + end end end end