@@ -22,6 +22,20 @@ def self.entity_name
2222 expose :title , documentation : { type : 'string' , desc : 'Title of the kind.' }
2323 expose :something , documentation : { type : Something , desc : 'Something interesting.' }
2424 end
25+
26+ class Base < Grape ::Entity
27+ def self . entity_name
28+ parts = to_s . split ( '::' )
29+
30+ "MyAPI::#{ parts . last } "
31+ end
32+
33+ expose :title , documentation : { type : 'string' , desc : 'Title of the parent.' }
34+ end
35+
36+ class Child < Base
37+ expose :child , documentation : { type : 'string' , desc : 'Child property.' }
38+ end
2539 end
2640
2741 class ResponseModelApi < Grape ::API
@@ -40,6 +54,16 @@ class ResponseModelApi < Grape::API
4054 present kind , with : Entities ::Kind
4155 end
4256
57+ desc 'This returns a child entity' ,
58+ entity : Entities ::Child ,
59+ http_codes : [
60+ { code : 200 , message : 'OK' , model : Entities ::Child }
61+ ]
62+ get '/child' do
63+ child = OpenStruct . new text : 'child'
64+ present child , with : Entities ::Child
65+ end
66+
4367 add_swagger_documentation # models: [MyAPI::Entities::Something, MyAPI::Entities::Kind]
4468 end
4569 end
@@ -49,36 +73,57 @@ def app
4973 MyAPI ::ResponseModelApi
5074 end
5175
52- subject do
53- get '/swagger_doc/kind'
54- JSON . parse ( last_response . body )
76+ describe 'kind' do
77+ subject do
78+ get '/swagger_doc/kind'
79+ JSON . parse ( last_response . body )
80+ end
81+
82+ it 'should document specified models' do
83+ expect ( subject [ 'paths' ] [ '/kind' ] [ 'get' ] [ 'parameters' ] ) . to eq [ {
84+ 'in' => 'query' ,
85+ 'name' => 'something' ,
86+ 'description' => 'Something interesting.' ,
87+ 'type' => 'SomethingCustom' ,
88+ 'required' => false
89+ } ]
90+
91+ expect ( subject [ 'definitions' ] . keys ) . to include 'SomethingCustom'
92+ expect ( subject [ 'definitions' ] [ 'SomethingCustom' ] ) . to eq (
93+ 'type' => 'object' , 'properties' => { 'text' => { 'type' => 'string' , 'description' => 'Content of something.' } }
94+ )
95+
96+ expect ( subject [ 'definitions' ] . keys ) . to include 'KindCustom'
97+ expect ( subject [ 'definitions' ] [ 'KindCustom' ] ) . to eq (
98+ 'type' => 'object' ,
99+ 'properties' => {
100+ 'title' => { 'type' => 'string' , 'description' => 'Title of the kind.' } ,
101+ 'something' => {
102+ '$ref' => '#/definitions/SomethingCustom' ,
103+ 'description' => 'Something interesting.'
104+ }
105+ } ,
106+ 'description' => 'This returns kind and something or an error'
107+ )
108+ end
55109 end
56110
57- it 'should document specified models' do
58- expect ( subject [ 'paths' ] [ '/kind' ] [ 'get' ] [ 'parameters' ] ) . to eq [ {
59- 'in' => 'query' ,
60- 'name' => 'something' ,
61- 'description' => 'Something interesting.' ,
62- 'type' => 'SomethingCustom' ,
63- 'required' => false
64- } ]
65-
66- expect ( subject [ 'definitions' ] . keys ) . to include 'SomethingCustom'
67- expect ( subject [ 'definitions' ] [ 'SomethingCustom' ] ) . to eq (
68- 'type' => 'object' , 'properties' => { 'text' => { 'type' => 'string' , 'description' => 'Content of something.' } }
69- )
70-
71- expect ( subject [ 'definitions' ] . keys ) . to include 'KindCustom'
72- expect ( subject [ 'definitions' ] [ 'KindCustom' ] ) . to eq (
73- 'type' => 'object' ,
74- 'properties' => {
75- 'title' => { 'type' => 'string' , 'description' => 'Title of the kind.' } ,
76- 'something' => {
77- '$ref' => '#/definitions/SomethingCustom' ,
78- 'description' => 'Something interesting.'
79- }
80- } ,
81- 'description' => 'This returns kind and something or an error'
82- )
111+ describe 'child' do
112+ subject do
113+ get '/swagger_doc/child'
114+ JSON . parse ( last_response . body )
115+ end
116+
117+ it 'should document specified models' do
118+ expect ( subject [ 'definitions' ] . keys ) . to include 'MyAPI::Child'
119+ expect ( subject [ 'definitions' ] [ 'MyAPI::Child' ] ) . to eq (
120+ 'type' => 'object' ,
121+ 'properties' => {
122+ 'title' => { 'type' => 'string' , 'description' => 'Title of the parent.' } ,
123+ 'child' => { 'type' => 'string' , 'description' => 'Child property.' }
124+ } ,
125+ 'description' => 'This returns a child entity'
126+ )
127+ end
83128 end
84129end
0 commit comments