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 ordering issues between as: and default: validations #2177

Merged
merged 5 commits into from
May 5, 2021

Conversation

Catsuko
Copy link
Contributor

@Catsuko Catsuko commented May 4, 2021

Fixes #2094 where it was found that defining as: before default: would fail to add the default whereas as: after default: would work fine.

@Catsuko Catsuko marked this pull request as ready for review May 4, 2021 17:43
@Catsuko Catsuko changed the title Issue 2094 Fix ordering issues between as: and default: validations May 4, 2021
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.

Close! Thank you.

CHANGELOG.md Outdated
@@ -7,6 +7,7 @@
#### Fixes

* [#2176](https://github.com/ruby-grape/grape/pull/2176): Fixes issue-2175 - options call failing if matching all routes - [@myxoh](https://github.com/myxoh).
* [#2177](https://github.com/ruby-grape/grape/pull/2177): Fixes issue-2094 - Issue with the order of `as:` and `default:` options - [@Catsuko](https://github.com/Catsuko).
Copy link
Member

Choose a reason for hiding this comment

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

Not sure how we inherited this pattern, but we don't usually have one :), change this to Fix: actual specific problem between as: and default: validators, and for the line above Fix: OPTIONS fails if matching all routes. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good, I reworded it so hopefully it describes it better


# Before we run the rest of the validators, let's handle
# whatever coercion so that we are working with correctly
# type casted values
coerce_type validations, attrs, doc_attrs, opts

as = validations.delete(:as)
Copy link
Member

Choose a reason for hiding this comment

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

The validations arg that is being passed into the function should not be modified to avoid side effects, make a copy.

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 refactored my changes to avoid mutating the arg however it is still being done within the method outside of my changes:

 desc = validations.delete(:desc) || validations.delete(:description)
# ...
 doc_attrs[:documentation] = validations.delete(:documentation) if validations.key?(:documentation)

I'd like to try refactoring however the scope seems a little scary so perhaps as part of its own issue.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. And thank you, do give it a shot.

def validates_presence(validations, attrs, doc_attrs, opts)
return unless validations.key?(:presence) && validations[:presence]
validate(:presence, validations[:presence], attrs, doc_attrs, opts)
validations.delete(:presence)
Copy link
Member

Choose a reason for hiding this comment

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

Same as above, don't alter validates, make a copy. The caller may be removing things if needed.

@Catsuko Catsuko requested a review from dblock May 5, 2021 02:02
@dblock dblock merged commit fe0f78b into ruby-grape:master May 5, 2021
@dblock
Copy link
Member

dblock commented May 5, 2021

Thanks for fixing this, merged. It could be an overkill, but maybe we should introduce some kind of Rack-style listing/mounting of validators so that we don't have to do special ordering gymnastics at runtime. Just food for thought.

@Catsuko
Copy link
Contributor Author

Catsuko commented May 14, 2021

@dblock

I think it'd make the behaviour more consistent too, perhaps there are other validators that don't play nicely with having :default before or after. I'd like to give it a go after I do more reading\thinking about it.

@Catsuko Catsuko deleted the issue-2094 branch September 15, 2022 16:02
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.

Issue with the order of as: and default: options on param declarations
2 participants