Skip to content

Commit 33366da

Browse files
dslhLeFnord
andauthored
Parameter examples (#821)
Co-authored-by: peter scholz <pscholz.le@gmail.com>
1 parent ed07baf commit 33366da

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ add_swagger_documentation \
451451
* [Collection Format](#collection-format)
452452
* [Hiding parameters](#hiding-parameters)
453453
* [Setting a Swagger default value](#default-value)
454+
* [Example parameter value](#param-example)
454455
* [Response documentation](#response)
455456
* [Changing default status codes](#change-status)
456457
* [File response](#file-response)
@@ -769,8 +770,6 @@ params do
769770
end
770771
```
771772

772-
The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
773-
774773
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.
775774

776775
```ruby
@@ -780,6 +779,16 @@ params do
780779
end
781780
```
782781

782+
#### Example parameter value <a name="param-example"></a>
783+
784+
The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
785+
786+
```ruby
787+
params do
788+
requires :id, type: Integer, documentation: { example: 123 }
789+
optional :name, type String, documentation: { example: 'Buddy Guy' }
790+
end
791+
```
783792

784793
#### Expose nested namespace as standalone route
785794

lib/grape-swagger/doc_methods/parse_params.rb

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def call(param, settings, path, route, definitions)
2727
document_required(settings)
2828
document_additional_properties(settings)
2929
document_add_extensions(settings)
30+
document_example(settings)
3031

3132
@parsed_param
3233
end
@@ -112,6 +113,11 @@ def document_additional_properties(settings)
112113
@parsed_param[:additionalProperties] = additional_properties if additional_properties
113114
end
114115

116+
def document_example(settings)
117+
example = settings[:example]
118+
@parsed_param[:example] = example if example
119+
end
120+
115121
def param_type(value_type)
116122
param_type = value_type[:param_type] || value_type[:in]
117123
if value_type[:path].include?("{#{value_type[:param_name]}}")
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'Param example' do
6+
def app
7+
Class.new(Grape::API) do
8+
format :json
9+
10+
params do
11+
requires :id, type: Integer, documentation: { example: 123 }
12+
optional :name, type: String, documentation: { example: 'Person' }
13+
optional :obj, type: 'Object', documentation: { example: { 'foo' => 'bar' } }
14+
end
15+
16+
get '/endpoint_with_examples' do
17+
{ 'declared_params' => declared(params) }
18+
end
19+
20+
add_swagger_documentation
21+
end
22+
end
23+
24+
describe 'documentation with parameter examples' do
25+
subject do
26+
get '/swagger_doc/endpoint_with_examples'
27+
JSON.parse(last_response.body)
28+
end
29+
30+
specify do
31+
expect(subject['paths']['/endpoint_with_examples']['get']['parameters']).to eql(
32+
[
33+
{ 'in' => 'query', 'name' => 'id', 'type' => 'integer', 'example' => 123, 'format' => 'int32', 'required' => true },
34+
{ 'in' => 'query', 'name' => 'name', 'type' => 'string', 'example' => 'Person', 'required' => false },
35+
{ 'in' => 'query', 'name' => 'obj', 'type' => 'Object', 'example' => { 'foo' => 'bar' }, 'required' => false }
36+
]
37+
)
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)