From 1ddf61dd902f5db64846d73175f63f74ffd2f168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Emin=20=C4=B0NA=C3=87?= Date: Thu, 26 Aug 2021 16:54:13 +0200 Subject: [PATCH] Fix infinite loop problem with the `NameError`s 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 https://github.com/ruby/did_you_mean/issues/158 * Fix rubocop cop violations --- lib/grape_entity/entity.rb | 2 +- spec/grape_entity/entity_spec.rb | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 9324e3ee..7b1de79a 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -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) diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index 8121fd5b..026b05b5 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -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 @@ -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 @@ -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(