@@ -7,13 +7,25 @@ def app
7
7
Class . new ( Grape ::API ) do
8
8
format :json
9
9
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
+
10
18
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 ) }
14
23
end
15
24
16
- get '/endpoint_with_examples' do
25
+ params do
26
+ use :common_params
27
+ end
28
+ post '/endpoint_with_examples/:id' do
17
29
{ 'declared_params' => declared ( params ) }
18
30
end
19
31
@@ -27,14 +39,34 @@ def app
27
39
JSON . parse ( last_response . body )
28
40
end
29
41
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
38
70
end
39
71
end
40
72
end
0 commit comments