Skip to content

Commit a315284

Browse files
author
dancostalis
committed
#215 - Adds example parameter to set swaggerDefault value without a Grape default
1 parent a0ab20d commit a315284

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

.rubocop_todo.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2015-03-11 10:53:03 -0400 using RuboCop version 0.27.0.
2+
# on 2015-03-12 10:07:36 -0500 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: 347
10+
Max: 357
1111

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

1717
# Offense count: 6
1818
Metrics/CyclomaticComplexity:
19-
Max: 99
19+
Max: 105
2020

21-
# Offense count: 294
21+
# Offense count: 297
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: 369
29+
Max: 377
3030

3131
# Offense count: 5
3232
Metrics/PerceivedComplexity:
33-
Max: 101
33+
Max: 107
3434

3535
# Offense count: 8
3636
Style/ClassVars:

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 0.10.2 (Next)
22

3+
* [#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).
34
* Your contribution here.
45

56
### 0.10.1 (March 11, 2015)

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,28 @@ The name should not contain whitespaces or any other special characters due to f
229229
end
230230
end
231231

232+
## Additional documentation
233+
234+
## Setting a Swagger defaultValue
235+
236+
Grape allows for an additional documentation hash to be passed to a parameter.
237+
238+
params do
239+
requires :id, type: Integer, desc: 'Coffee ID'
240+
requires :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', documentation: { example: 72 }
241+
end
242+
243+
The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
244+
245+
Grape uses the option `default` to set a default value for optional parameters. This is different in that Grape will set your parameter to the provided default if the parameter is omitted, whereas the example value above will only set the value in the UI itself. This will set the Swagger `defaultValue` to the provided value. Note that the example value will override the Grape default value.
246+
247+
248+
params do
249+
requires :id, type: Integer, desc: 'Coffee ID'
250+
optional :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', default: 72
251+
end
252+
253+
232254
## Grape Entities
233255

234256
Add the [grape-entity](https://github.com/agileanimal/grape-entity) gem to our Gemfile.

lib/grape-swagger.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,17 @@ def parse_params(params, path, method)
200200
else
201201
@@documentation_class.parse_entity_name(raw_data_type)
202202
end
203+
204+
additional_documentation = value.is_a?(Hash) ? value[:documentation] : nil
205+
206+
if additional_documentation && value.is_a?(Hash)
207+
value = additional_documentation.merge(value)
208+
end
209+
203210
description = value.is_a?(Hash) ? value[:desc] || value[:description] : ''
204211
required = value.is_a?(Hash) ? !!value[:required] : false
205212
default_value = value.is_a?(Hash) ? value[:default] : nil
213+
example = value.is_a?(Hash) ? value[:example] : nil
206214
is_array = value.is_a?(Hash) ? (value[:is_array] || false) : false
207215
enum_values = value.is_a?(Hash) ? value[:values] : nil
208216
enum_values = enum_values.to_a if enum_values && enum_values.is_a?(Range)
@@ -238,10 +246,14 @@ def parse_params(params, path, method)
238246
required: required,
239247
allowMultiple: is_array
240248
}
249+
241250
parsed_params.merge!(format: 'int32') if data_type == 'integer'
242251
parsed_params.merge!(format: 'int64') if data_type == 'long'
243252
parsed_params.merge!(items: items) if items.present?
244-
parsed_params.merge!(defaultValue: default_value) if default_value
253+
parsed_params.merge!(defaultValue: example) if example
254+
if default_value && example.blank?
255+
parsed_params.merge!(defaultValue: default_value)
256+
end
245257
parsed_params.merge!(enum: enum_values) if enum_values
246258
parsed_params
247259
end

spec/grape-swagger_helper_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ class HelperTestAPI < Grape::API
2424
]
2525
end
2626

27+
it 'parses params as query strings for a GET with an example' do
28+
params = {
29+
name: { type: 'String', desc: 'A name', required: true, default: 'default api value', documentation: { example: 'default swagger value' } },
30+
level: 'max'
31+
}
32+
path = '/coolness'
33+
method = 'GET'
34+
expect(subject.parse_params(params, path, method)).to eq [
35+
{ paramType: 'query', name: :name, description: 'A name', type: 'string', required: true, allowMultiple: false, defaultValue: 'default swagger value' },
36+
{ paramType: 'query', name: :level, description: '', type: 'string', required: false, allowMultiple: false }
37+
]
38+
end
39+
2740
it 'parses params as form for a POST' do
2841
params = {
2942
name: { type: 'String', desc: 'A name', required: true },

0 commit comments

Comments
 (0)