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 ValuesValidator for params wrapped in with block #2382

Merged
merged 2 commits into from
Dec 6, 2023

Conversation

numbata
Copy link
Contributor

@numbata numbata commented Dec 5, 2023

This PR fixes a bug #2376 in Grape's ValuesValidator, specifically when parameters are nested within a with block. Previously, the validator did not correctly handle the required validation for nested parameters in this context.

Changes:

  • The required_for_root_scope? method in ValuesValidator has been modified to correctly detect if a parameter is required when it's nested inside a with block. The fix involves checking the parent scope of the parameter until a non-lateral (non-with block) scope is found, and then determining if it's the root scope.
  • A test case has been added to values_spec.rb to cover the scenario where a parameter nested within a with block doesn't meet the validation criteria, ensuring that the correct error response is generated.

Impact:

This fix ensures more accurate validation of parameters nested within with blocks, and aligns the behavior with the expected norms of Grape's parameter validation. It's an important fix for Grape users who use nested parameter structures, improving the reliability and correctness of the validation logic.

I welcome any feedback or suggestions on this fix and look forward to your reviews!

@@ -85,7 +85,12 @@ def except_message
end

def required_for_root_scope?
@required && @scope.root?
return false unless @required
Copy link
Contributor Author

@numbata numbata Dec 5, 2023

Choose a reason for hiding this comment

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

"fail fast" if the param is not required.

Comment on lines +90 to +91
scope = @scope
scope = scope.parent while scope.lateral?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the logic here to handle nested parameters within a with block. The method now traverses up the scope hierarchy to check if the parameter's root scope is required, ensuring correct validation even when parameters are deeply nested within a `with' block.

Copy link
Member

Choose a reason for hiding this comment

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

You could extract this into lateral_scope or something like that, although I am not sure it's worth it

@numbata numbata changed the title Fix ValuesValidator for Params Wrapped in with Block in Grape Fix ValuesValidator for params wrapped in with block Dec 5, 2023
@dblock
Copy link
Member

dblock commented Dec 6, 2023

Perfect, thank you. Update CHANGELOG and we're good to go!

@dblock dblock merged commit 4e3b5fd into ruby-grape:master Dec 6, 2023
31 checks passed
@dblock
Copy link
Member

dblock commented Dec 6, 2023

Merged, thank you.

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.

2 participants