Skip to content

Commit 1ddf61d

Browse files
authored
Fix infinite loop problem with the NameErrors in block exposures (#355)
* Fix infinite loop problem with the `NameError`s in block exposures Calling `NameError#message` causes the `did_you_mean` library to endup being in an infinite loop(Probabaly raises `SystemStackError` at some point though). Related to ruby/did_you_mean#158 * Fix rubocop cop violations
1 parent 675d3c0 commit 1ddf61d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/grape_entity/entity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ def exec_with_object(options, &block)
527527
# it handles: https://github.com/ruby/ruby/blob/v3_0_0_preview1/NEWS.md#language-changes point 3, Proc
528528
raise Grape::Entity::Deprecated.new e.message, 'in ruby 3.0' if e.is_a?(ArgumentError)
529529

530-
raise e.class, e.message
530+
raise e
531531
end
532532

533533
def exec_with_attribute(attribute, &block)

spec/grape_entity/entity_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030

3131
it 'makes sure that :format_with as a proc cannot be used with a block' do
3232
# rubocop:disable Style/BlockDelimiters
33-
# rubocop:disable Lint/EmptyBlock
3433
expect { subject.expose :name, format_with: proc {} do p 'hi' end }.to raise_error ArgumentError
35-
# rubocop:enable Lint/EmptyBlock
3634
# rubocop:enable Style/BlockDelimiters
3735
end
3836

@@ -1137,6 +1135,18 @@ class Parent < Person
11371135
expect(representation).to eq(id: nil, name: nil, user: { id: nil, name: nil, email: nil })
11381136
end
11391137
end
1138+
1139+
context 'when NameError happens in a parameterized block_exposure' do
1140+
before do
1141+
subject.expose :raise_no_method_error do |_|
1142+
foo
1143+
end
1144+
end
1145+
1146+
it 'does not cause infinite loop' do
1147+
expect { subject.represent({}, serializable: true) }.to raise_error(NameError)
1148+
end
1149+
end
11401150
end
11411151
end
11421152

@@ -1581,7 +1591,7 @@ class NoPathCharacterEntity < Grape::Entity
15811591
end
15821592

15831593
fresh_class.class_eval do
1584-
expose :characteristics, using: EntitySpec::NoPathCharacterEntity, attr_path: proc { nil }
1594+
expose :characteristics, using: EntitySpec::NoPathCharacterEntity, attr_path: proc {}
15851595
end
15861596

15871597
expect(subject.serializable_hash).to eq(

0 commit comments

Comments
 (0)