Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Apipie接收整数参数作为字符串_Ruby On Rails_Rspec_Ruby On Rails 5_Rspec Rails_Apipie - Fatal编程技术网

Ruby on rails Apipie接收整数参数作为字符串

Ruby on rails Apipie接收整数参数作为字符串,ruby-on-rails,rspec,ruby-on-rails-5,rspec-rails,apipie,Ruby On Rails,Rspec,Ruby On Rails 5,Rspec Rails,Apipie,我进行了以下RSpec测试: 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 它失败了,因为Apipie抱怨

我进行了以下RSpec测试:

 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
它失败了,因为Apipie抱怨
workspace\u id
是一个字符串,而实际上不是,它是一个整数。我调试了调用,检查了
@workspace
,并且
id
肯定是一个整数

现在我看到了这个问题,因为我正在将应用程序迁移到Rails 5.2.0(以前是Rails 4)


有人见过这样的情况吗?

当您试图发送一些有效负载时,
GET
请求不包含body。在
GET
请求作为url查询传递的所有参数的情况下(例如
/index?model_id=1&workspace_id=1
),所有参数都是字符串

这里有两个选项:

  • GET
    更改为
    POST
    ,它将允许带正文的请求
  • 在操作中将字符串转换为整数

  • 是的。我遇到了同样的问题。我将Rack::Test与rspec一起使用,Rack::Test自动(bug)将每个值转换为字符串。不确定还有哪些后端位也这样做

    我发现让rack test将字符串以外的任何内容作为值发送的唯一方法是重写如下所述的请求方法:

    还有一个问题是,BigDecimal值被“as::json”强制转换成字符串,这不会影响OP(id是整数),但其他人可能通过搜索找到这个问题。修复该bug的解决方案被Rails团队贬低并删除。因此,为了能够遵循JSON消费者可能期望的JSON约定(vs Rails约定),您需要覆盖BigDecimal-as_JSON方法来修复它:

    没有。apipie验证实际上可以看到workspace_id为1,但将其视为一个整数,因此
    参数
    不是通过主体发送的,而是通过查询参数发送的。url查询参数中没有类型,所有url查询参数都是字符串。谁知道,这是一个开源库,请检查更改日志,问题,也许发生了一些变化。