如何设置参数类型' body'在葡萄招摇?

时间:2014-08-12 21:42:13

标签: ruby-on-rails swagger grape

在Swagger中你有Post或Put选项在正文中输入JSON。你有一个很好的模型和模型架构。 几个小时后我一直在努力寻找如何使用Rails,Grape和Grape-Swagger来实现这一点。文档是非常基本的以及示例。任何帮助赞赏。

这就是我现在所拥有的:

    params do
      requires :ticket_id, type: Integer, desc: "Ticket ID (case insensitive)"
      requires :body, type: String, desc: "ticket body" # <-- type should be body or something like this
    end
    put ':ticket_id' do
      ticket= Ticket.where(ticket_id: params[:ticket_id]).first
      if params[:body]
        body = JSON.parse(params[:body])
        description = body.map { |m| m[:description] }
        ticket_state = body.map { |m| m[:ticketState] }
        unless description.nil? or ticket_state.nil?
          ticket.description += description
          ticket.ticket_state = ticket_state
          ticket.change_date = Time.now
          ticket.save
        end
      end
      present ticket, with: API::V1::Entities::Ticket
    end

2 个答案:

答案 0 :(得分:1)

使用葡萄酒招摇,葡萄和葡萄实体的github master分支,以下内容适用于我:

class API::Entities::Request::Body < Grape::Entity
  expose :id, documentation: { type: 'integer', required: true }
end

class API::Entities::ResponseEntity< Grape::Entity
  expose :id, documentation: { type: 'integer', required: true }
end

desc 'Create batch',
      entity: API::Entities::ResponseEntity
params do
  optional :request, type: API::Entities::Request::Body, desc: '...'
end
post '/test' do
transformed_params = API::Entities::Request::Body.represent(params, serializable: true)
 {}
end

答案 1 :(得分:1)

我不知道在创建原始帖子时这是否可行但是今天我解决了这个问题:

params do
  optional :time, type: String, desc: 'Time string (ISO8601)', documentation: { param_type: 'body' }
end

它在正文中接受这个JSON:

{
  "time": "2012-04-23T18:25:43.511Z"
}