Skip to content

Commit

Permalink
Corrected a hash modification while iterating issue.
Browse files Browse the repository at this point in the history
Signed-off-by: Hermann Mayer <hermann.mayer92@gmail.com>
  • Loading branch information
Jack12816 committed Feb 22, 2021
1 parent 6a21f80 commit c859eee
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Your contribution here.

* [#2161](https://github.com/ruby-grape/grape/pull/2157): Handle EOFError from Rack when given an empty multipart body - [@bschmeck](https://github.com/bschmeck).
* [#2162](https://github.com/ruby-grape/grape/pull/2162): Corrected a hash modification while iterating issue - [@Jack12816](https://github.com/Jack12816).

### 1.5.2 (2021/02/06)

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def instance_for_rack
# Adds a new stage to the set up require to get a Grape::API up and running
def add_setup(method, *args, &block)
setup_step = { method: method, args: args, block: block }
@setup << setup_step
@setup += [setup_step]
last_response = nil
@instances.each do |instance|
last_response = replay_step_on(instance, setup_step)
Expand Down
77 changes: 77 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4056,4 +4056,81 @@ def before
expect { get '/const/missing' }.to raise_error(NameError).with_message(/SomeRandomConstant/)
end
end

describe 'custom route helpers on nested APIs' do
let(:shared_api_module) do
Module.new do
# rubocop:disable Style/ExplicitBlockArgument because this causes
# the underlying issue in this form
def uniqe_id_route
params do
use :unique_id
end
route_param(:id) do
yield
end
end
# rubocop:enable Style/ExplicitBlockArgument
end
end
let(:shared_api_definitions) do
Module.new do
extend ActiveSupport::Concern

included do
helpers do
params :unique_id do
requires :id, type: String,
allow_blank: false,
regexp: /\d+-\d+/
end
end
end
end
end
let(:orders_root) do
shared = shared_api_definitions
find = orders_find_endpoint
Class.new(Grape::API) do
include shared

namespace(:orders) do
mount find
end
end
end
let(:orders_find_endpoint) do
shared = shared_api_definitions
Class.new(Grape::API) do
include shared

uniqe_id_route do
desc 'Fetch a single order' do
detail 'While specifying the order id on the route'
end
get { params[:id] }
end
end
end
subject(:grape_api) do
Class.new(Grape::API) do
version 'v1', using: :path
end
end

before do
Grape::API::Instance.extend(shared_api_module)
subject.mount orders_root
end

it 'returns an error when the id is bad' do
get '/v1/orders/abc'
expect(last_response.body).to be_eql('id is invalid')
end

it 'returns the given id when it is valid' do
get '/v1/orders/1-2'
expect(last_response.body).to be_eql('1-2')
end
end
end

0 comments on commit c859eee

Please sign in to comment.