Skip to content

Commit 94a3fee

Browse files
blakenumbata
authored andcommitted
add tests
1 parent c7170da commit 94a3fee

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

lib/grape-swagger/openapi_3/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def response_body_object(_, _, parameters)
208208
properties = v.map { |value| [value[:name], value.except(:name, :in, :required, :schema).merge(value[:schema])] }.to_h
209209
required_values = v.select { |param| param[:required] }.map { |required| required[:name] }
210210
result = { 'schema' => { 'type' => 'object', 'properties' => properties } }
211-
result['required'] = required_values unless required_values.empty?
211+
result['schema']['required'] = required_values unless required_values.empty?
212212
['application/x-www-form-urlencoded', result]
213213
end.to_h
214214
}

spec/openapi_3/api_openapi_3_response_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,24 @@ def app
114114
end
115115

116116
specify do
117+
fail("TODO: Fix")
117118
expect(subject['paths']['/params_given']['post']).to eql(
118119
'description' => 'This returns something',
119-
'parameters' => [
120-
{ 'in' => 'formData', 'name' => 'description', 'type' => 'string', 'required' => false },
121-
{ 'in' => 'formData', 'name' => '$responses', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false }
122-
],
120+
'requestBody' => {
121+
'content' => {
122+
'application/x-www-form-urlencoded' => {
123+
'schema' => {
124+
'properties' => {
125+
'$responses' => {
126+
'items' => { 'type' => 'string' }, 'type' => 'array'
127+
},
128+
'description' => { 'type' => 'string' }
129+
},
130+
'type' => 'object'
131+
}
132+
}
133+
}
134+
},
123135
'responses' => {
124136
'201' => {
125137
'description' => 'This returns something'

spec/openapi_3/param_type_spec.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'Params Types' do
6+
def app
7+
Class.new(Grape::API) do
8+
format :json
9+
10+
params do
11+
requires :input, type: String
12+
end
13+
post :action do
14+
end
15+
16+
params do
17+
requires :input, type: String, default: '14', documentation: { type: 'email', default: '42' }
18+
end
19+
post :action_with_doc do
20+
end
21+
22+
add_swagger_documentation openapi_version: '3.0'
23+
end
24+
end
25+
context 'with no documentation hash' do
26+
subject do
27+
get '/swagger_doc/action'
28+
expect(last_response.status).to eq 200
29+
body = JSON.parse last_response.body
30+
body['paths']['/action']['post']
31+
end
32+
33+
it 'reads param type correctly' do
34+
expect(subject['requestBody']).to eq 'content' => {
35+
'application/x-www-form-urlencoded' => {
36+
'schema' => {
37+
'properties' => {
38+
'input' => { 'type' => 'string' }
39+
},
40+
'required' => ['input'],
41+
'type' => 'object'
42+
}
43+
}
44+
}
45+
end
46+
47+
describe 'header params' do
48+
def app
49+
Class.new(Grape::API) do
50+
format :json
51+
52+
desc 'Some API', headers: { 'My-Header' => { required: true, description: 'Set this!' } }
53+
params do
54+
requires :input, type: String
55+
end
56+
post :action do
57+
end
58+
59+
add_swagger_documentation openapi_version: '3.0'
60+
end
61+
end
62+
63+
it 'has consistent types' do
64+
parameter_type = subject['parameters'].map { |param| param['schema']['type'] }
65+
expect(parameter_type).to eq(%w[string])
66+
67+
header_type = subject['requestBody']['content']['application/x-www-form-urlencoded']['schema']['properties'].values.map { |param| param['type'] }
68+
expect(header_type).to eq(%w[string])
69+
end
70+
end
71+
end
72+
73+
context 'with documentation hash' do
74+
subject do
75+
get '/swagger_doc/action_with_doc'
76+
expect(last_response.status).to eq 200
77+
body = JSON.parse last_response.body
78+
body['paths']['/action_with_doc']['post']['requestBody']
79+
end
80+
81+
it 'reads param type correctly' do
82+
expect(subject).to eq 'content' => {
83+
'application/x-www-form-urlencoded' => {
84+
'schema' => {
85+
'properties' => {
86+
'input' => { 'default' => '42', 'format' => 'email', 'type' => 'string' }
87+
},
88+
'required' => ['input'], 'type' => 'object'
89+
}
90+
}
91+
}
92+
end
93+
end
94+
end

0 commit comments

Comments
 (0)