Apipie receiving integer param as String

纵然是瞬间 提交于 2020-01-25 10:14:17

问题


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 to POST, 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!