-
-
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
Define Boolean for Grape::API::Instance #1900
Define Boolean for Grape::API::Instance #1900
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.
Do you think you can make an integration test for this?
CHANGELOG.md
Outdated
@@ -9,6 +9,7 @@ | |||
* Your contribution here. | |||
* [#1893](https://github.com/ruby-grape/grape/pull/1893): Allows `Grape::API` to behave like a Rack::app in some instances where it was misbehaving - [@myxoh](https://github.com/myxoh). | |||
* [#1898](https://github.com/ruby-grape/grape/pull/1898): Refactor ValidatorFactory to improve memory allocation - [@Bhacaz](https://github.com/Bhacaz). | |||
* [#1900](https://github.com/ruby-grape/grape/pull/1900): Define boolean for grape::api::instance - [@Bhacaz](https://github.com/Bhacaz). |
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.
Correct the casing of Grape::Api::Instance
and quote it. Also quote ValidatorFactory
above while we're at it, please.
The test added doesn't test the original problem though, no? This succeeds with or without the code change? it 'works' do
class EchoAPI < Grape::API::Instance
params do
requires :message, type: Boolean
end
post :echo do
params[:message]
end
end |
The spec failed if I remove the line Maybe I don't follow how the specs is usually done in this project. I open to suggestions... |
Your spec tests that you define require 'spec_helper'
describe Grape::API::Instance do
describe 'boolean constant' do
subject do
Class.new(Grape::API::Instance) do
params do
requires :message, type: Boolean
end
post :echo do
params[:message]
end
end
end
def app
subject
end
it 'sets Boolean as a Virtus::Attribute::Boolean' do
get '/echo?message=true'
...
end
end
end |
@dblock First of all, thank you to have taken the time to try/rewrite this spec. I try it and without surprise like you said it failed. So I change |
Ok, this works for me, thank you. Can I please nitpick on the spec:
|
post '/echo?message=true' | ||
expect(last_response.status).to eq(201) | ||
expect(last_response.body).to eq expected_body | ||
expect(DefinesBooleanInstanceSpec::API.new.router.map['POST'].first.options[:params]['message'][:type]).to eq 'Virtus::Attribute::Boolean' |
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.
Nitpick, write a separate spec with this line and put params
as subject
or let
. I'll stop after this I promise :)
I was wondering why this actually worked for |
I saw it but I didn't realize it was in |
I merged this. Thanks for hanging in there! I think the spec is fine where it is. Feel free to make proposals/suggestions/move things around if you think otherwise! |
When using the
Grape::API::Instance
the constantBoolean
isn't declare.Example:
Will raise:
main.rb:9:in `block in <class:EchoAPI>': uninitialized constant EchoAPI::Boolean (NameError)
The work around is to use
requires :message, type: Virtus::Attribute::Boolean
, but it's not very consistent withGrape::API
and it was declare has an issue #1144.Maybe there's a better implementation to fix the issue, like in the
Grape::DSL::Helpers
(done for the issue here #1153).