diff --git a/lib/grape/dsl/parameters.rb b/lib/grape/dsl/parameters.rb index e774105a8a..bfc9b408e3 100644 --- a/lib/grape/dsl/parameters.rb +++ b/lib/grape/dsl/parameters.rb @@ -170,7 +170,8 @@ def optional(*attrs, &block) # @param (see #requires) # @option (see #requires) def with(*attrs, &block) - new_group_scope(attrs.clone, &block) + new_group_attrs = [@group, attrs.clone.first].compact.reduce(&:deep_merge) + new_group_scope([new_group_attrs], &block) end # Disallow the given parameters to be present in the same request. diff --git a/spec/grape/dsl/parameters_spec.rb b/spec/grape/dsl/parameters_spec.rb index 5106866d09..6590e4adaa 100644 --- a/spec/grape/dsl/parameters_spec.rb +++ b/spec/grape/dsl/parameters_spec.rb @@ -35,6 +35,12 @@ def validates_reader @validates end + def new_scope(args, _, &block) + nested_scope = self.class.new + nested_scope.new_group_scope(args, &block) + nested_scope + end + def new_group_scope(args) @group = args.clone.first yield @@ -169,6 +175,37 @@ def extract_message_option(attrs) ] ) end + + it "supports nested 'with' calls" do + subject.with(type: Integer, documentation: { in: 'body' }) do + subject.optional :pipboy_id + subject.with(documentation: { default: 33 }) do + subject.optional :vault + end + end + + expect(subject.validate_attributes_reader).to eq( + [ + [:pipboy_id], { type: Integer, documentation: { in: 'body' } }, + [:vault], { type: Integer, documentation: { in: 'body', default: 33 } } + ] + ) + end + + it "supports Hash parameter inside the 'with' calls" do + subject.with(documentation: { in: 'body' }) do + subject.optional :info, type: Hash, documentation: { x: { nullable: true }, desc: 'The info' } do + subject.optional :vault, type: Integer, documentation: { default: 33, desc: 'The vault number' } + end + end + + expect(subject.validate_attributes_reader).to eq( + [ + [:info], { type: Hash, documentation: { in: 'body', desc: 'The info', x: { nullable: true } } }, + [:vault], { type: Integer, documentation: { in: 'body', default: 33, desc: 'The vault number' } } + ] + ) + end end describe '#mutually_exclusive' do