Skip to content

Commit c943989

Browse files
Use x-example for non-body parameters
1 parent e0282ab commit c943989

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

lib/grape-swagger/doc_methods/parse_params.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ def parse_additional_properties(definitions, settings)
141141
end
142142

143143
def document_example(settings)
144-
example = settings[:example]
145-
@parsed_param[:example] = example if example
144+
return unless settings.key?(:example)
145+
146+
key = @parsed_param[:in] == 'body' ? :example : :'x-example'
147+
@parsed_param[key] = settings[:example]
146148
end
147149

148150
def param_type(value_type, consumes)

spec/swagger_v2/params_example_spec.rb

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@ def app
77
Class.new(Grape::API) do
88
format :json
99

10+
helpers do
11+
params :common_params do
12+
requires :id, type: Integer, documentation: { example: 123 }
13+
optional :name, type: String, documentation: { example: 'Person' }
14+
optional :obj, type: 'Object', documentation: { example: { 'foo' => 'bar' } }
15+
end
16+
end
17+
1018
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' } }
19+
use :common_params
20+
end
21+
get '/endpoint_with_examples/:id' do
22+
{ 'declared_params' => declared(params) }
1423
end
1524

16-
get '/endpoint_with_examples' do
25+
params do
26+
use :common_params
27+
end
28+
post '/endpoint_with_examples/:id' do
1729
{ 'declared_params' => declared(params) }
1830
end
1931

@@ -27,14 +39,34 @@ def app
2739
JSON.parse(last_response.body)
2840
end
2941

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-
)
42+
describe 'examples for non-body parameters ' do
43+
specify do
44+
expect(subject['paths']['/endpoint_with_examples/{id}']['get']['parameters']).to eql(
45+
[
46+
{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'x-example' => 123, 'format' => 'int32', 'required' => true },
47+
{ 'in' => 'query', 'name' => 'name', 'type' => 'string', 'x-example' => 'Person', 'required' => false },
48+
{ 'in' => 'query', 'name' => 'obj', 'type' => 'Object', 'x-example' => { 'foo' => 'bar' }, 'required' => false }
49+
]
50+
)
51+
end
52+
end
53+
54+
describe 'examples for body parameters' do
55+
specify do
56+
expect(subject['paths']['/endpoint_with_examples/{id}']['post']['parameters']).to eql(
57+
[
58+
{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'x-example' => 123, 'format' => 'int32', 'required' => true },
59+
{ 'in' => 'body', 'name' => 'postEndpointWithExamplesId', 'schema' => { '$ref' => '#/definitions/postEndpointWithExamplesId' }, 'required' => true }
60+
]
61+
)
62+
expect(subject['definitions']['postEndpointWithExamplesId']).to eql(
63+
'type' => 'object',
64+
'properties' => {
65+
'name' => { 'type' => 'string', 'example' => 'Person' },
66+
'obj' => { 'type' => 'Object', 'example' => { 'foo' => 'bar' } }
67+
}
68+
)
69+
end
3870
end
3971
end
4072
end

0 commit comments

Comments
 (0)