Skip to content

Commit 98316ed

Browse files
committed
Corrected a hash modification while iterating issue.
Signed-off-by: Hermann Mayer <[email protected]>
1 parent 6a21f80 commit 98316ed

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

lib/grape/api.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def instance_for_rack
141141
# Adds a new stage to the set up require to get a Grape::API up and running
142142
def add_setup(method, *args, &block)
143143
setup_step = { method: method, args: args, block: block }
144-
@setup << setup_step
144+
@setup += [setup_step]
145145
last_response = nil
146146
@instances.each do |instance|
147147
last_response = replay_step_on(instance, setup_step)

spec/grape/api_spec.rb

+77
Original file line numberDiff line numberDiff line change
@@ -4056,4 +4056,81 @@ def before
40564056
expect { get '/const/missing' }.to raise_error(NameError).with_message(/SomeRandomConstant/)
40574057
end
40584058
end
4059+
4060+
fdescribe 'custom route helpers on nested APIs' do
4061+
let(:shared_api_module) do
4062+
Module.new do
4063+
# rubocop:disable Style/ExplicitBlockArgument because this causes
4064+
# the underlying issue in this form
4065+
def uniqe_id_route
4066+
params do
4067+
use :unique_id
4068+
end
4069+
route_param(:id) do
4070+
yield
4071+
end
4072+
end
4073+
# rubocop:enable Style/ExplicitBlockArgument
4074+
end
4075+
end
4076+
let(:shared_api_definitions) do
4077+
Module.new do
4078+
extend ActiveSupport::Concern
4079+
4080+
included do
4081+
helpers do
4082+
params :unique_id do
4083+
requires :id, type: String,
4084+
allow_blank: false,
4085+
regexp: /\d+-\d+/
4086+
end
4087+
end
4088+
end
4089+
end
4090+
end
4091+
let(:orders_root) do
4092+
shared = shared_api_definitions
4093+
find = orders_find_endpoint
4094+
Class.new(Grape::API) do
4095+
include shared
4096+
4097+
namespace(:orders) do
4098+
mount find
4099+
end
4100+
end
4101+
end
4102+
let(:orders_find_endpoint) do
4103+
shared = shared_api_definitions
4104+
Class.new(Grape::API) do
4105+
include shared
4106+
4107+
uniqe_id_route do
4108+
desc 'Fetch a single order' do
4109+
detail 'While specifying the order id on the route'
4110+
end
4111+
get { params[:id] }
4112+
end
4113+
end
4114+
end
4115+
subject(:grape_api) do
4116+
Class.new(Grape::API) do
4117+
version 'v1', using: :path
4118+
end
4119+
end
4120+
4121+
before do
4122+
Grape::API::Instance.extend(shared_api_module)
4123+
subject.mount orders_root
4124+
end
4125+
4126+
it 'returns an error when the id is bad' do
4127+
get '/v1/orders/abc'
4128+
expect(last_response.body).to be_eql('id is invalid')
4129+
end
4130+
4131+
it 'returns the given id when it is valid' do
4132+
get '/v1/orders/1-2'
4133+
expect(last_response.body).to be_eql('1-2')
4134+
end
4135+
end
40594136
end

0 commit comments

Comments
 (0)