Grape Swagger describes JSON body

I am sending a JSON request for most POST requests and I am inserting JSON into the request body. How can I describe this using a virtuoso wedding?

+3


source to share


2 answers


Adding descriptions for the parameters is very easy (I assume this is what you mean by "How can I describe this with viticulture?"). Ivan shows you how to create params blocks using Hash or Array, but to describe them using Grape Swagger you need to add a description parameter to the required or optional parameter in the params block.

class API::Users < Grape::API
  resource 'user' do
    desc 'Create a new user'
    params do 
      requires :user, type: Hash, desc: 'user object to create' do 
        requires :first_name, type: String, desc: 'First name of user'
        requires :last_name, type: String, desc: 'Last name of user'
      end
    end
    post do 
      # do work here
    end
  end
end

      



Both desc to the endpoint and desc keys in the hash sent to requires

will be picked up by Grape Swagger and included in the automatic documentation.

+1


source


You can use Array or Hash to describe

params do
 optional :preferences, type: Array do
  requires :key
  requires :value
 end

 requires :name, type: Hash do
  requires :first_name
  requires :last_name
 end
end

      

An array for when you need to store has_many objects. A hash when you only need to store the object.



Example:

params do
  requires :post, type: Hash do
    requires :title
    requires :description
    optional :images_attributes, type: Array
  end
end

      

0


source







All Articles