@@ -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
4072end
0 commit comments