Skip to content

Commit 99b1430

Browse files
Levente Bagidblock
Levente Bagi
authored andcommitted
Added responseModel support.
1 parent b5a0a8b commit 99b1430

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
### Next Release
22

3+
* [#110](https://github.com/tim-vandecasteele/grape-swagger/pull/110) - Added `responseModel` support - [@bagilevi](https://github.com/bagilevi).
4+
* [#105](https://github.com/tim-vandecasteele/grape-swagger/pull/105): Fixed compatibility with Swagger-UI - [@CraigCottingham](https://github.com/CraigCottingham).
5+
* [#87](https://github.com/tim-vandecasteele/grape-swagger/pull/87): Fixed mapping of `default` to `defaultValue` - [@m-o-e](https://github.com/m-o-e).
36
* Rewritten .gemspec and removed Jeweler - [@dblock](https://github.com/dblock).
47
* Added `GrapeEntity::VERSION` - [@dblock](https://github.com/dblock).
58
* Added Rubocop, Ruby-style linter - [@dblock](https://github.com/dblock).
6-
* [#105](https://github.com/tim-vandecasteele/grape-swagger/pull/105): Fixed compatibility with Swagger-UI - [@CraigCottingham](https://github.com/CraigCottingham).
7-
* [#87](https://github.com/tim-vandecasteele/grape-swagger/pull/87): Fixed mapping of `default` to `defaultValue` - [@m-o-e](https://github.com/m-o-e).
89
* Your Contribution Here
910

1011
### 0.7.2 (February 6, 2014)

lib/grape-swagger.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def self.setup(options)
131131
ops.each do |path, op_routes|
132132
operations = op_routes.map do |route|
133133
notes = as_markdown(route.route_notes)
134-
http_codes = parse_http_codes(route.route_http_codes)
134+
http_codes = parse_http_codes(route.route_http_codes, models)
135135

136136
models << if @@models.present?
137137
@@models
@@ -374,14 +374,16 @@ def generate_typeref(type)
374374
end
375375
end
376376

377-
def parse_http_codes(codes)
377+
def parse_http_codes(codes, models)
378378
codes ||= {}
379-
codes.map do |k, v|
380-
{
379+
codes.map do |k, v, m|
380+
models << m if m
381+
code = {
381382
code: k,
382-
message: v,
383-
# responseModel: ...
383+
message: v
384384
}
385+
code[:responseModel] = parse_entity_name(m) if m
386+
code
385387
end
386388
end
387389

spec/response_model_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper'
2+
3+
describe 'responseModel' do
4+
before :all do
5+
module Entities
6+
class Something < Grape::Entity
7+
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
8+
end
9+
10+
class Error < Grape::Entity
11+
expose :code, documentation: { type: 'string', desc: 'Error code' }
12+
expose :message, documentation: { type: 'string', desc: 'Error message' }
13+
end
14+
end
15+
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+
]
24+
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+
end
33+
end
34+
35+
add_swagger_documentation
36+
end
37+
end
38+
39+
def app
40+
ResponseModelApi
41+
end
42+
43+
it 'should document specified models' do
44+
get '/swagger_doc/something'
45+
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+
])
58+
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+
)
66+
end
67+
end

0 commit comments

Comments
 (0)