Skip to content

Accept :using as a string #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [#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).
* [#178](https://github.com/tim-vandecasteele/grape-swagger/issues/178): Fixed `Hash` parameters, now exposed as Swagger `object` types - [@dblock](https://github.com/dblock).
* [#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).
* [#185](https://github.com/tim-vandecasteele/grape-swagger/pull/185): Support strings in `Grape::Entity.expose`'s `:using` option - [@jhollinger](https://github.com/jhollinger).
* Your contribution here.

### 0.8.0 (August 30, 2014)
Expand Down
5 changes: 4 additions & 1 deletion lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ def models_with_included_presenters(models)
models.each do |model|
# get model references from exposures with a documentation
nested_models = model.exposures.map do |_, config|
config[:using] if config.key?(:documentation)
if config.key?(:documentation)
model = config[:using]
model.respond_to?(:constantize) ? model.constantize : model
end
end.compact

# get all nested models recursively
Expand Down
16 changes: 15 additions & 1 deletion spec/response_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class Kind < Grape::Entity
class Something < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
expose :kind, using: Kind, documentation: { type: 'MyAPI::Kind', desc: 'The kind of this something.' }
expose :relation, using: 'MyAPI::Entities::Relation', documentation: { type: 'MyAPI::Relation', desc: 'A related model.' }
end

class Relation < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end

class Error < Grape::Entity
Expand Down Expand Up @@ -82,7 +87,8 @@ def app
'id' => 'MyAPI::Something',
'properties' => {
'text' => { 'type' => 'string', 'description' => 'Content of something.' },
'kind' => { '$ref' => 'MyAPI::Kind', 'description' => 'The kind of this something.' }
'kind' => { '$ref' => 'MyAPI::Kind', 'description' => 'The kind of this something.' },
'relation' => { '$ref' => 'MyAPI::Relation', 'description' => 'A related model.' }
}
)

Expand All @@ -93,5 +99,13 @@ def app
'title' => { 'type' => 'string', 'description' => 'Title of the kind.' }
}
)

expect(subject['models'].keys).to include 'MyAPI::Relation'
expect(subject['models']['MyAPI::Relation']).to eq(
'id' => 'MyAPI::Relation',
'properties' => {
'name' => { 'type' => 'string', 'description' => 'Name' }
}
)
end
end