Skip to content

Commit daaad4c

Browse files
zsxkingdblock
authored andcommitted
Fix param_type override
1 parent ac2c43e commit daaad4c

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

.rubocop_todo.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2015-03-12 10:07:36 -0500 using RuboCop version 0.27.0.
2+
# on 2015-03-13 06:55:39 -0400 using RuboCop version 0.27.0.
33
# The point is for the user to remove these configuration records
44
# one by one as the offenses are removed from the code base.
55
# Note that changes in the inspected code, or installation of new
66
# versions of RuboCop, may require this file to be generated again.
77

88
# Offense count: 10
99
Metrics/AbcSize:
10-
Max: 358
10+
Max: 360
1111

1212
# Offense count: 1
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 498
15+
Max: 496
1616

1717
# Offense count: 6
1818
Metrics/CyclomaticComplexity:
1919
Max: 106
2020

21-
# Offense count: 297
21+
# Offense count: 300
2222
# Configuration parameters: AllowURI, URISchemes.
2323
Metrics/LineLength:
2424
Max: 254
2525

2626
# Offense count: 20
2727
# Configuration parameters: CountComments.
2828
Metrics/MethodLength:
29-
Max: 379
29+
Max: 377
3030

3131
# Offense count: 5
3232
Metrics/PerceivedComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* [#215](https://github.com/tim-vandecasteele/grape-swagger/pull/223): Support swagger `defaultValue` without the need to set a Grape `default` - [@jv-dan](https://github.com/jv-dan).
6+
* [#225](https://github.com/tim-vandecasteele/grape-swagger/pull/225): Fixed param_type to have it read from parameter's documentation hash - [@zsxking](https://github.com/zsxking).
67
* Your contribution here.
78

89
#### Fixes

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ You can specify a swagger nickname to use instead of the auto generated name by
191191
desc 'Get a full list of pets', nickname: 'getAllPets'
192192
```
193193

194+
## Overriding param type
195+
196+
You can override paramType in POST|PUT methods to query, using the documentation hash.
197+
198+
``` ruby
199+
params do
200+
requires :action, type: Symbol, values: [:PAUSE, :RESUME, :STOP], documentation: { param_type: 'query' }
201+
end
202+
post :act do
203+
...
204+
end
205+
```
206+
194207
## Expose nested namespace as standalone route
195208
Use the `nested: false` property in the `swagger` option to make nested namespaces appear as standalone resources.
196209
This option can help to structure and keep the swagger schema simple.

lib/grape-swagger.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ def parse_params(params, path, method)
218218
enum_values = enum_values.to_a if enum_values && enum_values.is_a?(Range)
219219
enum_values = enum_values.call if enum_values && enum_values.is_a?(Proc)
220220

221-
if value.is_a?(Hash) && value.key?(:param_type)
222-
param_type = value[:param_type]
221+
if value.is_a?(Hash) && value.key?(:documentation) && value[:documentation].key?(:param_type)
222+
param_type = value[:documentation][:param_type]
223223
if is_array
224224
items = { '$ref' => data_type }
225225
data_type = 'array'

spec/param_type_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
3+
describe 'Params Types' do
4+
def app
5+
Class.new(Grape::API) do
6+
format :json
7+
8+
params do
9+
requires :input, type: String, documentation: { param_type: 'query' }
10+
end
11+
post :action do
12+
end
13+
14+
add_swagger_documentation
15+
end
16+
end
17+
18+
subject do
19+
get '/swagger_doc/action'
20+
expect(last_response.status).to eq 200
21+
body = JSON.parse last_response.body
22+
body['apis'].first['operations'].first['parameters']
23+
end
24+
25+
it 'reads param type correctly' do
26+
expect(subject).to eq [
27+
{ 'paramType' => 'query', 'name' => 'input', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false }
28+
]
29+
end
30+
end

0 commit comments

Comments
 (0)