|
2 | 2 |
|
3 | 3 | describe 'responseModel' do
|
4 | 4 | before :all do
|
5 |
| - module Entities |
6 |
| - class Something < Grape::Entity |
7 |
| - expose :text, documentation: { type: 'string', desc: 'Content of something.' } |
8 |
| - end |
| 5 | + module MyAPI |
| 6 | + module Entities |
| 7 | + class BaseEntity < Grape::Entity |
| 8 | + def self.entity_name |
| 9 | + name.sub(/^MyAPI::Entities::/, '') |
| 10 | + end |
| 11 | + end |
9 | 12 |
|
10 |
| - class Error < Grape::Entity |
11 |
| - expose :code, documentation: { type: 'string', desc: 'Error code' } |
12 |
| - expose :message, documentation: { type: 'string', desc: 'Error message' } |
| 13 | + class Something < BaseEntity |
| 14 | + expose :text, documentation: { type: 'string', desc: 'Content of something.' } |
| 15 | + end |
| 16 | + |
| 17 | + class Error < BaseEntity |
| 18 | + expose :code, documentation: { type: 'string', desc: 'Error code' } |
| 19 | + expose :message, documentation: { type: 'string', desc: 'Error message' } |
| 20 | + end |
13 | 21 | end
|
14 |
| - end |
15 | 22 |
|
16 |
| - class ResponseModelApi < Grape::API |
17 |
| - format :json |
18 |
| - desc 'This returns something or an error', |
19 |
| - entity: Entities::Something, |
20 |
| - http_codes: [ |
21 |
| - [200, 'OK', Entities::Something], |
22 |
| - [403, 'Refused to return something', Entities::Error] |
23 |
| - ] |
| 23 | + class ResponseModelApi < Grape::API |
| 24 | + format :json |
| 25 | + desc 'This returns something or an error', |
| 26 | + entity: Entities::Something, |
| 27 | + http_codes: [ |
| 28 | + [200, 'OK', Entities::Something], |
| 29 | + [403, 'Refused to return something', Entities::Error] |
| 30 | + ] |
24 | 31 |
|
25 |
| - get '/something/:id' do |
26 |
| - if params[:id] == 1 |
27 |
| - something = OpenStruct.new text: 'something' |
28 |
| - present something, with: Entities::Something |
29 |
| - else |
30 |
| - error = OpenStruct.new code: 'some_error', message: 'Some error' |
31 |
| - present error, with: Entities::Error |
| 32 | + get '/something/:id' do |
| 33 | + if params[:id] == 1 |
| 34 | + something = OpenStruct.new text: 'something' |
| 35 | + present something, with: Entities::Something |
| 36 | + else |
| 37 | + error = OpenStruct.new code: 'some_error', message: 'Some error' |
| 38 | + present error, with: Entities::Error |
| 39 | + end |
32 | 40 | end
|
33 |
| - end |
34 | 41 |
|
35 |
| - add_swagger_documentation |
| 42 | + add_swagger_documentation |
| 43 | + end |
36 | 44 | end
|
37 | 45 | end
|
38 | 46 |
|
39 | 47 | def app
|
40 |
| - ResponseModelApi |
| 48 | + MyAPI::ResponseModelApi |
41 | 49 | end
|
42 | 50 |
|
43 | 51 | it 'should document specified models' do
|
44 | 52 | get '/swagger_doc/something'
|
45 | 53 | parsed_response = JSON.parse(last_response.body)
|
46 |
| - parsed_response['apis'][0]['operations'][0]['responseMessages'].should eq([ |
47 |
| - { |
48 |
| - 'code' => 200, |
49 |
| - 'message' => 'OK', |
50 |
| - 'responseModel' => 'Something' |
51 |
| - }, |
52 |
| - { |
53 |
| - 'code' => 403, |
54 |
| - 'message' => 'Refused to return something', |
55 |
| - 'responseModel' => 'Error' |
56 |
| - } |
57 |
| - ]) |
| 54 | + parsed_response['apis'][0]['operations'][0]['responseMessages'].should eq( |
| 55 | + [ |
| 56 | + { |
| 57 | + 'code' => 200, |
| 58 | + 'message' => 'OK', |
| 59 | + 'responseModel' => 'Something' |
| 60 | + }, |
| 61 | + { |
| 62 | + 'code' => 403, |
| 63 | + 'message' => 'Refused to return something', |
| 64 | + 'responseModel' => 'Error' |
| 65 | + } |
| 66 | + ] |
| 67 | + ) |
58 | 68 | parsed_response['models'].keys.should include 'Error'
|
59 |
| - parsed_response['models']['Error'].should eq( |
60 |
| - 'id' => 'Error', |
61 |
| - 'properties' => { |
62 |
| - 'code' => { 'type' => 'string', 'description' => 'Error code' }, |
63 |
| - 'message' => { 'type' => 'string', 'description' => 'Error message' } |
64 |
| - } |
65 |
| - ) |
| 69 | + parsed_response['models']['Error'].should == { |
| 70 | + 'id' => 'Error', |
| 71 | + 'properties' => { |
| 72 | + 'code' => { 'type' => 'string', 'description' => 'Error code' }, |
| 73 | + 'message' => { 'type' => 'string', 'description' => 'Error message' } |
| 74 | + } |
| 75 | + } |
66 | 76 | end
|
67 | 77 | end
|
0 commit comments