Skip to content

Commit

Permalink
Nested with support
Browse files Browse the repository at this point in the history
  • Loading branch information
numbata committed Apr 26, 2024
1 parent 96c2a91 commit 2e389db
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions spec/grape/dsl/parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e389db

Please sign in to comment.