|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'Form Params' do |
| 6 | + def app |
| 7 | + Class.new(Grape::API) do |
| 8 | + format :json |
| 9 | + |
| 10 | + params do |
| 11 | + requires :name, type: String, desc: 'name of item' |
| 12 | + end |
| 13 | + post '/items' do |
| 14 | + {} |
| 15 | + end |
| 16 | + |
| 17 | + params do |
| 18 | + requires :id, type: Integer, desc: 'id of item' |
| 19 | + requires :name, type: String, desc: 'name of item' |
| 20 | + requires :conditions, type: Integer, desc: 'conditions of item', values: [1, 2, 3] |
| 21 | + end |
| 22 | + put '/items/:id' do |
| 23 | + {} |
| 24 | + end |
| 25 | + |
| 26 | + params do |
| 27 | + requires :id, type: Integer, desc: 'id of item' |
| 28 | + requires :name, type: String, desc: 'name of item' |
| 29 | + optional :conditions, type: String, desc: 'conditions of item', values: proc { %w[1 2] } |
| 30 | + end |
| 31 | + patch '/items/:id' do |
| 32 | + {} |
| 33 | + end |
| 34 | + |
| 35 | + params do |
| 36 | + requires :id, type: Integer, desc: 'id of item' |
| 37 | + requires :name, type: String, desc: 'name of item' |
| 38 | + optional :conditions, type: Symbol, desc: 'conditions of item', values: %i[one two] |
| 39 | + end |
| 40 | + post '/items/:id' do |
| 41 | + {} |
| 42 | + end |
| 43 | + |
| 44 | + add_swagger_documentation openapi_version: '3.0' |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + subject do |
| 49 | + get '/swagger_doc/items' |
| 50 | + puts last_response.body |
| 51 | + JSON.parse(last_response.body) |
| 52 | + end |
| 53 | + |
| 54 | + it 'retrieves the documentation form params' do |
| 55 | + expect(subject['paths'].length).to eq 2 |
| 56 | + expect(subject['paths'].keys).to include('/items', '/items/{id}') |
| 57 | + expect(subject['paths']['/items'].keys).to include 'post' |
| 58 | + expect(subject['paths']['/items/{id}'].keys).to include('post', 'patch', 'put') |
| 59 | + end |
| 60 | + |
| 61 | + it 'treats Symbol parameter as form param' do |
| 62 | + expect(subject['paths']['/items/{id}']['post']['parameters']).to eq [{ |
| 63 | + 'in' => 'path', |
| 64 | + 'name' => 'id', |
| 65 | + 'description' => 'id of item', |
| 66 | + 'required' => true, |
| 67 | + 'schema' => { 'type' => 'integer', 'format' => 'int32' } |
| 68 | + }] |
| 69 | + |
| 70 | + expect(subject['paths']['/items/{id}']['post']['requestBody']).to eq 'content' => { |
| 71 | + 'application/x-www-form-urlencoded' => { |
| 72 | + 'schema' => { |
| 73 | + 'properties' => { |
| 74 | + 'conditions' => { |
| 75 | + 'description' => 'conditions of item', |
| 76 | + 'enum' => %w[one two], |
| 77 | + 'type' => 'string' |
| 78 | + }, |
| 79 | + 'name' => { |
| 80 | + 'description' => 'name of item', |
| 81 | + 'type' => 'string' |
| 82 | + } |
| 83 | + }, |
| 84 | + 'required' => ['name'], |
| 85 | + 'type' => 'object' |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + end |
| 90 | +end |
0 commit comments