Skip to content

Commit d33fb9d

Browse files
committed
Merge pull request #185 from jhollinger/using-string
Accept :using as a string
2 parents 86ed8df + fd65d91 commit d33fb9d

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [#179](https://github.com/tim-vandecasteele/grape-swagger/pull/179): Document `Virtus::Attribute::Boolean` as boolean - [@eashman](https://github.com/eashman), [@dblock](https://github.com/dblock).
1111
* [#178](https://github.com/tim-vandecasteele/grape-swagger/issues/178): Fixed `Hash` parameters, now exposed as Swagger `object` types - [@dblock](https://github.com/dblock).
1212
* [#167](https://github.com/tim-vandecasteele/grape-swagger/pull/167): Support mutli-tenanted APIs, don't cache `base_path` - [@bradrobertson](https://github.com/bradrobertson), (https://github.com/dblock).
13+
* [#185](https://github.com/tim-vandecasteele/grape-swagger/pull/185): Support strings in `Grape::Entity.expose`'s `:using` option - [@jhollinger](https://github.com/jhollinger).
1314
* Your contribution here.
1415

1516
### 0.8.0 (August 30, 2014)

lib/grape-swagger.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,10 @@ def models_with_included_presenters(models)
258258
models.each do |model|
259259
# get model references from exposures with a documentation
260260
nested_models = model.exposures.map do |_, config|
261-
config[:using] if config.key?(:documentation)
261+
if config.key?(:documentation)
262+
model = config[:using]
263+
model.respond_to?(:constantize) ? model.constantize : model
264+
end
262265
end.compact
263266

264267
# get all nested models recursively

spec/response_model_spec.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class Kind < Grape::Entity
1111
class Something < Grape::Entity
1212
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
1313
expose :kind, using: Kind, documentation: { type: 'MyAPI::Kind', desc: 'The kind of this something.' }
14+
expose :relation, using: 'MyAPI::Entities::Relation', documentation: { type: 'MyAPI::Relation', desc: 'A related model.' }
15+
end
16+
17+
class Relation < Grape::Entity
18+
expose :name, documentation: { type: 'string', desc: 'Name' }
1419
end
1520

1621
class Error < Grape::Entity
@@ -82,7 +87,8 @@ def app
8287
'id' => 'MyAPI::Something',
8388
'properties' => {
8489
'text' => { 'type' => 'string', 'description' => 'Content of something.' },
85-
'kind' => { '$ref' => 'MyAPI::Kind', 'description' => 'The kind of this something.' }
90+
'kind' => { '$ref' => 'MyAPI::Kind', 'description' => 'The kind of this something.' },
91+
'relation' => { '$ref' => 'MyAPI::Relation', 'description' => 'A related model.' }
8692
}
8793
)
8894

@@ -93,5 +99,13 @@ def app
9399
'title' => { 'type' => 'string', 'description' => 'Title of the kind.' }
94100
}
95101
)
102+
103+
expect(subject['models'].keys).to include 'MyAPI::Relation'
104+
expect(subject['models']['MyAPI::Relation']).to eq(
105+
'id' => 'MyAPI::Relation',
106+
'properties' => {
107+
'name' => { 'type' => 'string', 'description' => 'Name' }
108+
}
109+
)
96110
end
97111
end

0 commit comments

Comments
 (0)