问题
I have the following RSpec test:
it 'should not list alerts, since I do not have access to this model' do
get :index, params: { model_id: @model2.id, workspace_id: @workspace.id }, as: :json
expect(response).to have_http_status(:forbidden)
end
and it is failing because Apipie is complaining the workspace_id
is a String when it is actually not, it is an Integer. I debugged the call, inspected @workspace
and id
is definitely an Integer.
I'm seeing this issue now that I'm migrating the application to Rails 5.2.0 (previously Rails 4).
Has anyone seen something like this?
回答1:
The GET
request doesn't contains body, while you're trying to send some payload. In the case of GET
request all params passed as url query (e.g. /index?model_id=1&workspace_id=1
) and all params are string.
You have two options here:
- Change
GET
toPOST
, it will allow request with body. - Convert string to integer in the action.
回答2:
Yes. I hit the same problem. I am using Rack::Test with rspec, and Rack::Test is automagically (bug) converting every value to a string. Not sure what other back-end bits also do this.
The only way I found to get rack-test to send anything other than strings as values, was by overriding the request methods as described here: https://stackoverflow.com/a/37234290/2326613
There is also a problem where BigDecimal values are being forced into strings by "as: :json" - not affecting the OP (IDs being Integers) but could be others finding this question by search. The workaround to fix that bug was depreciated and removed by the Rails team. So, to be able to follow JSON conventions which your JSON-consumer may expect (vs Rails conventions), you need to override the BigDecimal-as_json method to fix it: https://github.com/rails/rails/issues/25017
来源:https://stackoverflow.com/questions/52708351/apipie-receiving-integer-param-as-string