-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 attribute translator setter #2375
Fix attribute translator setter #2375
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, and yes, please add a test / CHANGELOG.
Generated by 🚫 Danger |
Not sure if failed tests are related, but I suspect they are. |
|
I am not against it. |
@Jell if you rebase your branch, all tests should pass. |
e195beb
to
2cbdb8b
Compare
@ericproulx @dblock I've rebased, added tests and a line in the changelog |
@dblock I thought I messed up my refactoring in 2019 but it always been like this since #1351. What @Jell did makes total sense but it changes the setter behavior. For instance, describe "#header=" do
subject { instance.header = 'new_value' }
let(:instance) { Grape::Router::AttributeTranslator.new(header: 'value') }
it "sets value for header" do
expect do
subject
puts "to_h = #{instance.to_h}"
end.to change(instance, :header).from('value').to('new_value')
end
end
On the other hand, I feel the setter methods aren't really used and everything is done through the initialize. For instance, removing the def method_missing(method_name, *args)
return attributes[method_name] unless setter?(method_name[-1])
super
end
def respond_to_missing?(method_name, _include_private = false)
@attributes.key?(method_name)
end Maybe we should consider removing the setter part and let the initialize do its work. After all, these attributes doesn't change after the APIs are compiled. |
@ericproulx FYI the reason for me doing the PR in the first place is because I wanted to set some default headers for Swagger documentation purposes after the route was compiled, something along the lines of: routes.each do |route|
add_default_headers(route)
add_default_response_codes(route)
end and I had to do some workaround like:
But I think it would also be OK if headers and http_codes where initialized with an empty collection? |
This was simply not working before.
2cbdb8b
to
da31f80
Compare
Good work @Jell! |
This was simply not working before. Doesn't seem like there are any unit tests for this class, so I haven't added new ones, but might be worth considering.