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

Corrected a hash modification while iterating issue. #2162

Merged
merged 1 commit into from
Feb 22, 2021

Conversation

Jack12816
Copy link
Contributor

- What is it good for

There is a nasty bug on edge case usage of nested mounted APIs which use a shared class-helper method to abstract the creation of a route. With this fix in place, no errors are raised. Otherwise a RuntimeError: can't add a new key into hash during iteration is raised. This is caused by an inline modification of the @setup set inside the Grape::API class. This error is caused by the definition of the helper method in a specific way. The following form causes the issue:

def uniqe_id_route
  params do
    use :unique_id
  end
  route_param(:id) do
    yield
  end
end  

while this form works as expected:

def uniqe_id_route(&block)
  params do
    use :unique_id
  end
  route_param(:id, &block)
end

- What I did

I just converted the inline set modification into a reassignment of a freshly built set and added a test that proves the bugfix.

- A picture of a cute animal (not mandatory but encouraged)

images

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this start adding duplicates into @setup? It's a Set.

spec/grape/api_spec.rb Outdated Show resolved Hide resolved
Signed-off-by: Hermann Mayer <hermann.mayer92@gmail.com>
@Jack12816
Copy link
Contributor Author

Doesn't this start adding duplicates into @setup? It's a Set.

No, it does not add duplicates, because it's a set. Example:

block1 = proc { false }
block2 = proc { true }

setup = Set.new
setup << { method: :default_format, args: [:json], block: block1 }
setup << { method: :default_format, args: [:text], block: block2 }
setup << { method: :default_format, args: [:text], block: block2 }
# =>
# #<Set: {{:method=>:default_format,
#   :args=>[:json],
#   :block=>#<Proc:0x000055bbcbe3a8f0 (irb):5>},
#  {:method=>:default_format,
#   :args=>[:text],
#   :block=>#<Proc:0x000055bbcb83dce0 (irb):6>}}>

setup = Set.new
setup += [{ method: :default_format, args: [:json], block: block1 }]
setup += [{ method: :default_format, args: [:text], block: block2 }]
setup += [{ method: :default_format, args: [:text], block: block2 }]
# #<Set: {{:method=>:default_format,
#   :args=>[:json],
#   :block=>#<Proc:0x000055bbcbe3a8f0 (irb):5>},
#  {:method=>:default_format,
#   :args=>[:text],
#   :block=>#<Proc:0x000055bbcb83dce0 (irb):6>}}>

@dblock dblock merged commit 8996fb1 into ruby-grape:master Feb 22, 2021
@dblock
Copy link
Member

dblock commented Feb 22, 2021

Looks good, thank you.

@marcusg
Copy link
Contributor

marcusg commented Mar 1, 2021

@dblock can you estimate when a new version can be released including this patch?

@dblock
Copy link
Member

dblock commented Mar 1, 2021

I can do it this week unless someone gets it before me, add your +1 to #2163

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants