Skip to content

Commit 6aaaeef

Browse files
blakenumbata
authored andcommitted
Add test
1 parent 6e16af5 commit 6aaaeef

File tree

1 file changed

+319
-0
lines changed

1 file changed

+319
-0
lines changed
Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'moving body/formData Params to definitions' do
6+
include_context "#{MODEL_PARSER} swagger example"
7+
8+
before :all do
9+
module TheApi
10+
class NestedBodyParamTypeApi < Grape::API
11+
namespace :simple_nested_params do
12+
desc 'post in body with nested parameters',
13+
detail: 'more details description',
14+
success: Entities::UseNestedWithAddress
15+
params do
16+
optional :contact, type: Hash do
17+
requires :name, type: String, documentation: { desc: 'name', in: 'body' }
18+
optional :addresses, type: Array do
19+
requires :street, type: String, documentation: { desc: 'street', in: 'body' }
20+
requires :postcode, type: String, documentation: { desc: 'postcode', in: 'body' }
21+
requires :city, type: String, documentation: { desc: 'city', in: 'body' }
22+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
23+
end
24+
end
25+
end
26+
27+
post '/in_body' do
28+
{ 'declared_params' => declared(params) }
29+
end
30+
31+
desc 'put in body with nested parameters',
32+
detail: 'more details description',
33+
success: Entities::UseNestedWithAddress
34+
params do
35+
requires :id, type: Integer
36+
optional :name, type: String, documentation: { desc: 'name', in: 'body' }
37+
optional :address, type: Hash do
38+
optional :street, type: String, documentation: { desc: 'street', in: 'body' }
39+
optional :postcode, type: String, documentation: { desc: 'postcode', in: 'formData' }
40+
optional :city, type: String, documentation: { desc: 'city', in: 'body' }
41+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
42+
end
43+
end
44+
45+
put '/in_body/:id' do
46+
{ 'declared_params' => declared(params) }
47+
end
48+
end
49+
50+
namespace :multiple_nested_params do
51+
desc 'put in body with multiple nested parameters',
52+
success: Entities::UseNestedWithAddress
53+
params do
54+
optional :contact, type: Hash do
55+
requires :name, type: String, documentation: { desc: 'name', in: 'body' }
56+
optional :addresses, type: Array do
57+
optional :street, type: String, documentation: { desc: 'street', in: 'body' }
58+
requires :postcode, type: Integer, documentation: { desc: 'postcode', in: 'formData' }
59+
optional :city, type: String, documentation: { desc: 'city', in: 'body' }
60+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
61+
end
62+
optional :delivery_address, type: Hash do
63+
optional :street, type: String, documentation: { desc: 'street', in: 'body' }
64+
optional :postcode, type: String, documentation: { desc: 'postcode', in: 'formData' }
65+
optional :city, type: String, documentation: { desc: 'city', in: 'body' }
66+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
67+
end
68+
end
69+
end
70+
71+
post '/in_body' do
72+
{ 'declared_params' => declared(params) }
73+
end
74+
75+
desc 'put in body with multiple nested parameters',
76+
success: Entities::UseNestedWithAddress
77+
params do
78+
requires :id, type: Integer
79+
optional :name, type: String, documentation: { desc: 'name', in: 'body' }
80+
optional :address, type: Hash do
81+
optional :street, type: String, documentation: { desc: 'street', in: 'body' }
82+
requires :postcode, type: String, documentation: { desc: 'postcode', in: 'formData' }
83+
optional :city, type: String, documentation: { desc: 'city', in: 'body' }
84+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
85+
end
86+
optional :delivery_address, type: Hash do
87+
optional :street, type: String, documentation: { desc: 'street', in: 'body' }
88+
optional :postcode, type: String, documentation: { desc: 'postcode', in: 'formData' }
89+
optional :city, type: String, documentation: { desc: 'city', in: 'body' }
90+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
91+
end
92+
end
93+
94+
put '/in_body/:id' do
95+
{ 'declared_params' => declared(params) }
96+
end
97+
end
98+
99+
add_swagger_documentation openapi_version: '3.0'
100+
end
101+
end
102+
end
103+
104+
def app
105+
TheApi::NestedBodyParamTypeApi
106+
end
107+
108+
describe 'nested body parameters given' do
109+
subject do
110+
get '/swagger_doc/simple_nested_params'
111+
JSON.parse(last_response.body)
112+
end
113+
114+
describe 'POST' do
115+
let(:endpoint) { subject['paths']['/simple_nested_params/in_body']['post'] }
116+
117+
specify do
118+
expect(endpoint['requestBody']['content']['application/json']).to eql(
119+
'schema' => {
120+
'properties' => {
121+
'SimpleNestedParamsInBody' => {
122+
'$ref' => '#/components/schemas/postSimpleNestedParamsInBody'
123+
}
124+
},
125+
'required' => ['SimpleNestedParamsInBody'], 'type' => 'object'
126+
}
127+
)
128+
end
129+
130+
specify do
131+
expect(subject['components']['schemas']['postSimpleNestedParamsInBody']).to eql(
132+
'type' => 'object',
133+
'properties' => {
134+
'contact' => {
135+
'type' => 'object',
136+
'properties' => {
137+
'name' => { 'type' => 'string', 'description' => 'name' },
138+
'addresses' => {
139+
'type' => 'array',
140+
'items' => {
141+
'type' => 'object',
142+
'properties' => {
143+
'street' => { 'type' => 'string', 'description' => 'street' },
144+
'postcode' => { 'type' => 'string', 'description' => 'postcode' },
145+
'city' => { 'type' => 'string', 'description' => 'city' },
146+
'country' => { 'type' => 'string', 'description' => 'country' }
147+
},
148+
'required' => %w[street postcode city]
149+
}
150+
}
151+
},
152+
'required' => %w[name]
153+
}
154+
},
155+
'description' => 'post in body with nested parameters'
156+
)
157+
end
158+
end
159+
160+
describe 'PUT' do
161+
let(:endpoint) { subject['paths']['/simple_nested_params/in_body/{id}']['put'] }
162+
163+
specify do
164+
expect(endpoint['parameters']).to eql(
165+
[{
166+
'in' => 'path', 'name' => 'id', 'schema' => { 'format' => 'int32', 'type' => 'integer' }, 'required' => true
167+
}]
168+
)
169+
170+
expect(endpoint['requestBody']['content']['application/json']).to eql(
171+
'schema' => {
172+
'properties' => {
173+
'SimpleNestedParamsInBody' => {
174+
'$ref' => '#/components/schemas/putSimpleNestedParamsInBody'
175+
}
176+
}, 'required' => ['SimpleNestedParamsInBody'], 'type' => 'object'
177+
}
178+
)
179+
end
180+
181+
specify do
182+
expect(subject['components']['schemas']['putSimpleNestedParamsInBody']).to eql(
183+
'type' => 'object',
184+
'properties' => {
185+
'name' => { 'type' => 'string', 'description' => 'name' },
186+
'address' => {
187+
'type' => 'object',
188+
'properties' => {
189+
'street' => { 'type' => 'string', 'description' => 'street' },
190+
'postcode' => { 'type' => 'string', 'description' => 'postcode' },
191+
'city' => { 'type' => 'string', 'description' => 'city' },
192+
'country' => { 'type' => 'string', 'description' => 'country' }
193+
}
194+
}
195+
},
196+
'description' => 'put in body with nested parameters'
197+
)
198+
end
199+
end
200+
end
201+
202+
describe 'multiple nested body parameters given' do
203+
subject do
204+
get '/swagger_doc/multiple_nested_params'
205+
JSON.parse(last_response.body)
206+
end
207+
208+
describe 'POST' do
209+
let(:endpoint) { subject['paths']['/multiple_nested_params/in_body']['post'] }
210+
211+
specify do
212+
expect(endpoint['requestBody']['content']['application/json']).to eql(
213+
'schema' => {
214+
'properties' => {
215+
'MultipleNestedParamsInBody' => {
216+
'$ref' => '#/components/schemas/postMultipleNestedParamsInBody'
217+
}
218+
},
219+
'required' => ['MultipleNestedParamsInBody'],
220+
'type' => 'object'
221+
}
222+
)
223+
end
224+
225+
specify do
226+
expect(subject['components']['schemas']['postMultipleNestedParamsInBody']).to eql(
227+
'type' => 'object',
228+
'properties' => {
229+
'contact' => {
230+
'type' => 'object',
231+
'properties' => {
232+
'name' => { 'type' => 'string', 'description' => 'name' },
233+
'addresses' => {
234+
'type' => 'array',
235+
'items' => {
236+
'type' => 'object',
237+
'properties' => {
238+
'street' => { 'type' => 'string', 'description' => 'street' },
239+
'postcode' => { 'type' => 'integer', 'format' => 'int32', 'description' => 'postcode' },
240+
'city' => { 'type' => 'string', 'description' => 'city' },
241+
'country' => { 'type' => 'string', 'description' => 'country' }
242+
},
243+
'required' => ['postcode']
244+
}
245+
},
246+
'delivery_address' => {
247+
'type' => 'object',
248+
'properties' => {
249+
'street' => { 'type' => 'string', 'description' => 'street' },
250+
'postcode' => { 'type' => 'string', 'description' => 'postcode' },
251+
'city' => { 'type' => 'string', 'description' => 'city' },
252+
'country' => { 'type' => 'string', 'description' => 'country' }
253+
}
254+
}
255+
},
256+
'required' => %w[name]
257+
}
258+
},
259+
'description' => 'put in body with multiple nested parameters'
260+
)
261+
end
262+
end
263+
264+
describe 'PUT' do
265+
let(:endpoint) { subject['paths']['/multiple_nested_params/in_body/{id}']['put'] }
266+
267+
specify do
268+
expect(endpoint['parameters']).to eql(
269+
[{
270+
'in' => 'path',
271+
'name' => 'id',
272+
'schema' => { 'format' => 'int32', 'type' => 'integer' },
273+
'required' => true
274+
}]
275+
)
276+
expect(endpoint['requestBody']['content']['application/json']).to eql(
277+
'schema' => {
278+
'properties' => {
279+
'MultipleNestedParamsInBody' => {
280+
'$ref' => '#/components/schemas/putMultipleNestedParamsInBody'
281+
}
282+
},
283+
'required' => ['MultipleNestedParamsInBody'],
284+
'type' => 'object'
285+
}
286+
)
287+
end
288+
289+
specify do
290+
expect(subject['components']['schemas']['putMultipleNestedParamsInBody']).to eql(
291+
'type' => 'object',
292+
'properties' => {
293+
'name' => { 'type' => 'string', 'description' => 'name' },
294+
'address' => {
295+
'type' => 'object',
296+
'properties' => {
297+
'street' => { 'type' => 'string', 'description' => 'street' },
298+
'postcode' => { 'type' => 'string', 'description' => 'postcode' },
299+
'city' => { 'type' => 'string', 'description' => 'city' },
300+
'country' => { 'type' => 'string', 'description' => 'country' }
301+
},
302+
'required' => ['postcode']
303+
},
304+
'delivery_address' => {
305+
'type' => 'object',
306+
'properties' => {
307+
'street' => { 'type' => 'string', 'description' => 'street' },
308+
'postcode' => { 'type' => 'string', 'description' => 'postcode' },
309+
'city' => { 'type' => 'string', 'description' => 'city' },
310+
'country' => { 'type' => 'string', 'description' => 'country' }
311+
}
312+
}
313+
},
314+
'description' => 'put in body with multiple nested parameters'
315+
)
316+
end
317+
end
318+
end
319+
end

0 commit comments

Comments
 (0)