|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'nested group params' do |
| 6 | + [true, false].each do |array_use_braces| |
| 7 | + context "when array_use_braces option is set to #{array_use_braces}" do |
| 8 | + let(:braces) { array_use_braces ? '[]' : '' } |
| 9 | + let(:app) do |
| 10 | + Class.new(Grape::API) do |
| 11 | + format :json |
| 12 | + |
| 13 | + params do |
| 14 | + requires :a_array, type: Array do |
| 15 | + requires :param_1, type: Integer |
| 16 | + requires :b_array, type: Array do |
| 17 | + requires :param_2, type: String |
| 18 | + end |
| 19 | + requires :c_hash, type: Hash do |
| 20 | + requires :param_3, type: String |
| 21 | + end |
| 22 | + end |
| 23 | + requires :a_array_foo, type: String |
| 24 | + end |
| 25 | + post '/nested_array' do |
| 26 | + { 'declared_params' => declared(params) } |
| 27 | + end |
| 28 | + |
| 29 | + params do |
| 30 | + requires :a_hash, type: Hash do |
| 31 | + requires :param_1, type: Integer |
| 32 | + requires :b_hash, type: Hash do |
| 33 | + requires :param_2, type: String |
| 34 | + end |
| 35 | + requires :c_array, type: Array do |
| 36 | + requires :param_3, type: String |
| 37 | + end |
| 38 | + end |
| 39 | + requires :a_hash_foo, type: String |
| 40 | + end |
| 41 | + post '/nested_hash' do |
| 42 | + { 'declared_params' => declared(params) } |
| 43 | + end |
| 44 | + |
| 45 | + add_swagger_documentation openapi_version: '3.0', array_use_braces: array_use_braces |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + describe 'retrieves the documentation for nested array parameters' do |
| 50 | + subject do |
| 51 | + get '/swagger_doc/nested_array' |
| 52 | + JSON.parse(last_response.body) |
| 53 | + end |
| 54 | + |
| 55 | + specify do |
| 56 | + expect(subject['paths']['/nested_array']['post']['requestBody']['content']['application/x-www-form-urlencoded']).to eql( |
| 57 | + 'schema' => { |
| 58 | + 'properties' => { |
| 59 | + "a_array#{braces}[b_array]#{braces}[param_2]" => { 'items' => { 'type' => 'string' }, 'type' => 'array' }, |
| 60 | + "a_array#{braces}[c_hash][param_3]" => { 'items' => { 'type' => 'string' }, 'type' => 'array' }, |
| 61 | + "a_array#{braces}[param_1]" => { 'format' => 'int32', 'items' => { 'type' => 'integer' }, 'type' => 'array' }, |
| 62 | + 'a_array_foo' => { 'type' => 'string' } |
| 63 | + }, |
| 64 | + 'required' => ["a_array#{braces}[param_1]", |
| 65 | + "a_array#{braces}[b_array]#{braces}[param_2]", |
| 66 | + "a_array#{braces}[c_hash][param_3]", |
| 67 | + 'a_array_foo'], |
| 68 | + 'type' => 'object' |
| 69 | + } |
| 70 | + ) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + describe 'retrieves the documentation for nested hash parameters' do |
| 75 | + subject do |
| 76 | + get '/swagger_doc/nested_hash' |
| 77 | + JSON.parse(last_response.body) |
| 78 | + end |
| 79 | + |
| 80 | + specify do |
| 81 | + expect(subject['paths']['/nested_hash']['post']['requestBody']['content']['application/x-www-form-urlencoded']).to eql( |
| 82 | + 'schema' => { |
| 83 | + 'properties' => { |
| 84 | + 'a_hash[b_hash][param_2]' => { 'type' => 'string' }, |
| 85 | + "a_hash[c_array]#{braces}[param_3]" => { 'items' => { 'type' => 'string' }, 'type' => 'array' }, |
| 86 | + 'a_hash[param_1]' => { 'format' => 'int32', 'type' => 'integer' }, |
| 87 | + 'a_hash_foo' => { 'type' => 'string' } |
| 88 | + }, |
| 89 | + 'required' => ['a_hash[param_1]', |
| 90 | + 'a_hash[b_hash][param_2]', |
| 91 | + "a_hash[c_array]#{braces}[param_3]", |
| 92 | + 'a_hash_foo'], |
| 93 | + 'type' => 'object' |
| 94 | + } |
| 95 | + ) |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments