Skip to content

Commit 40f7c31

Browse files
committed
Merge pull request #128 from dspaeth-faber/combine_global_models_with_enities
combined global models with endpoint entities
2 parents 383d838 + 10e903a commit 40f7c31

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Added Rubocop, Ruby-style linter - [@dblock](https://github.com/dblock).
1212
* [#126](https://github.com/tim-vandecasteele/grape-swagger/pull/126): Rewritten demo in the `test` folder with CORS enabled - [@dblock](https://github.com/dblock).
1313
* [#127](https://github.com/tim-vandecasteele/grape-swagger/pull/127): Fixed `undefined method 'reject' for nil:NilClass` error for an invalid route, now returning 404 Not Found - [@dblock](https://github.com/dblock).
14+
* [#128](https://github.com/tim-vandecasteele/grape-swagger/pull/128): Combine global models and endpoint entities - [@dspaeth-faber](https://github.com/dspaeth-faber).
1415
* Your Contribution Here
1516

1617
### 0.7.2 (February 6, 2014)

lib/grape-swagger.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,9 @@ def self.setup(options)
147147

148148
http_codes = parse_http_codes(route.route_http_codes, models)
149149

150-
models << if @@models.present?
151-
@@models
152-
elsif route.route_entity.present?
153-
route.route_entity
154-
end
150+
models << @@models if @@models.present?
151+
152+
models << route.route_entity if route.route_entity.present?
155153

156154
models = models_with_included_presenters(models.flatten.compact)
157155

spec/api_global_models_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ module Some
88
class Thing < Grape::Entity
99
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
1010
end
11+
12+
class CombinedThing < Grape::Entity
13+
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
14+
end
1115
end
1216
end
1317
end
@@ -20,6 +24,14 @@ class Thing < Grape::Entity
2024
present thing, with: Entities::Some::Thing
2125
end
2226

27+
desc 'This gets combined thing.',
28+
params: Entities::Some::CombinedThing.documentation,
29+
entity: Entities::Some::CombinedThing
30+
get '/combined_thing' do
31+
thing = OpenStruct.new text: 'thing'
32+
present thing, with: Entities::Some::CombinedThing
33+
end
34+
2335
add_swagger_documentation models: [Entities::Some::Thing]
2436
end
2537
end
@@ -39,4 +51,26 @@ def app
3951
}
4052
})
4153
end
54+
55+
it 'uses global models and route endpoint specific entities together' do
56+
get '/swagger_doc/combined_thing.json'
57+
json = JSON.parse(last_response.body)
58+
59+
expect(json['models']).to include(
60+
'Some::Thing' => {
61+
'id' => 'Some::Thing',
62+
'properties' => {
63+
'text' => { 'type' => 'string', 'description' => 'Content of something.' }
64+
}
65+
})
66+
67+
expect(json['models']).to include(
68+
'Some::CombinedThing' => {
69+
'id' => 'Some::CombinedThing',
70+
'properties' => {
71+
'text' => { 'type' => 'string', 'description' => 'Content of something.' }
72+
}
73+
})
74+
75+
end
4276
end

0 commit comments

Comments
 (0)