Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite loop problem with the NameErrors in block exposures #355

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def exec_with_object(options, &block)
# it handles: https://github.com/ruby/ruby/blob/v3_0_0_preview1/NEWS.md#language-changes point 3, Proc
raise Grape::Entity::Deprecated.new e.message, 'in ruby 3.0' if e.is_a?(ArgumentError)

raise e.class, e.message
raise e
end

def exec_with_attribute(attribute, &block)
Expand Down
16 changes: 13 additions & 3 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@

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

Expand Down Expand Up @@ -1137,6 +1135,18 @@ class Parent < Person
expect(representation).to eq(id: nil, name: nil, user: { id: nil, name: nil, email: nil })
end
end

context 'when NameError happens in a parameterized block_exposure' do
before do
subject.expose :raise_no_method_error do |_|
foo
end
end

it 'does not cause infinite loop' do
expect { subject.represent({}, serializable: true) }.to raise_error(NameError)
end
end
end
end

Expand Down Expand Up @@ -1581,7 +1591,7 @@ class NoPathCharacterEntity < Grape::Entity
end

fresh_class.class_eval do
expose :characteristics, using: EntitySpec::NoPathCharacterEntity, attr_path: proc { nil }
expose :characteristics, using: EntitySpec::NoPathCharacterEntity, attr_path: proc {}
end

expect(subject.serializable_hash).to eq(
Expand Down