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

refactor declared and include nil values for missing nested params #1575

Merged
merged 10 commits into from
Feb 17, 2017

Conversation

thogg4
Copy link
Contributor

@thogg4 thogg4 commented Feb 15, 2017

addresses #1574

@thogg4 thogg4 changed the title 1574 - test and change to include nil values for missing nested params include nil values for missing nested params in declared Feb 15, 2017
@thogg4 thogg4 changed the title include nil values for missing nested params in declared wip: include nil values for missing nested params in declared Feb 15, 2017
@thogg4 thogg4 changed the title wip: include nil values for missing nested params in declared refactor declared and include nil values for missing nested params Feb 16, 2017
@thogg4
Copy link
Contributor Author

thogg4 commented Feb 16, 2017

@dblock Tests are passing now. Can I get some feedback?

end
end
end

it 'should show nil for nested params if include_missing and include_missing_nested are true' do
Copy link
Member

Choose a reason for hiding this comment

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

There's no more include_missing_nested.

@@ -26,42 +26,56 @@ def self.post_filter_methods(type)
# Methods which should not be available in filters until the before filter
# has completed
module PostBeforeFilter
def declared(params, options = {}, declared_params = nil)
def declared(passed_params, options = {}, declared_params = nil)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe unrename passed_params to just params, will be easier to grok.

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 actually added this in because I thought it made it a little easier to grok.
When this is called and the declared child params params are passed as an argument, its a bit easier to remember that passed_params are always the params passed into the method and declared_params are the params that were declared in the params block

Copy link
Member

Choose a reason for hiding this comment

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

It's totally cool.


key.each_pair do |parent, children|
output_key = options[:stringify] ? parent.to_s : parent.to_sym
def declared_array(passed_params, options, declared_params)
Copy link
Member

Choose a reason for hiding this comment

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

These should probably be private.

next unless options[:include_missing] || params.key?(parent)
def declared_hash(passed_params, options, declared_params)
declared_params.each_with_object(Hashie::Mash.new) do |declared_param, memo|
# if it is not a Hash then it does not have children.
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick, this comment should not have a period if it's not a full sentence starting with a Capital.

it 'should show nil for nested params if include_missing and include_missing_nested are true' do
inner_params = nil
subject.get '/declared' do
inner_params = declared(params, include_missing: true)
Copy link
Member

Choose a reason for hiding this comment

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

I realize we do this elsewhere, but it's bad practice - maybe fix this and other instances?

This works, but is hacky and abuses the fact that the inner function still has access to outer variables. Below you check that the GET call succeeded with a 200, but then reach in for the value of inner_params. Be consistent, return declared(...) and examine the JSON response instead of relying on this.

@dblock
Copy link
Member

dblock commented Feb 17, 2017

I have fairly cosmetic change requests, otherwise this looks OK. Mostly trusting the fact that no existing specs broke :)

@thogg4
Copy link
Contributor Author

thogg4 commented Feb 17, 2017

@dblock Thanks for your comments, I have done the majority of those things. Please see my comment about naming params and let me know what you think.

@dblock dblock merged commit 7a56fe9 into ruby-grape:master Feb 17, 2017
@dblock
Copy link
Member

dblock commented Feb 17, 2017

Great work @thogg4, merged.

@thogg4 thogg4 deleted the include-missing-nested branch February 17, 2017 20:01
@tjwp
Copy link
Contributor

tjwp commented Feb 27, 2017

This change breaks one of our existing applications. We are using declared(params, include_missing: false) with params containing a nested Hash. There are params within the Hash that are optional without a default. With this change, the unspecified params are included in the Hash with nil values. Prior to this change, keys for the unspecified params are not present, and this is the behavior I would expect with include_missing: false.

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