From 3c6475ffb41470b026a64750c2cf3a79fd0deab9 Mon Sep 17 00:00:00 2001 From: Jordan Hollinger Date: Sat, 17 Jan 2015 13:41:34 -0500 Subject: [PATCH] If :type is omitted, see if it's available in :using --- CHANGELOG.md | 1 + README.md | 2 +- lib/grape-swagger.rb | 10 ++++++++-- spec/response_model_spec.rb | 10 ++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b7aeea5..82300e92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### Next Release +* [#196](https://github.com/tim-vandecasteele/grape-swagger/pull/196): If `:type` is omitted, see if it's available in `:using` - [@jhollinger](https://github.com/jhollinger). * Your contribution here. ### 0.9.0 (December 19, 2014) diff --git a/README.md b/README.md index 5bdb7aa4..57100bfd 100644 --- a/README.md +++ b/README.md @@ -229,7 +229,7 @@ end ### Relationships -Put the full name of the relationship's class in `type`, leaving out any modules named `Entities` or `Entity`. So for the entity class `API::Entities::Address`, you would put `type: 'API::Address'`. +You may safely omit `type` from relationships, as it can be inferred. However, if you need to specify or override it, use the full name of the class leaving out any modules named `Entities` or `Entity`. #### 1xN diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 52573374..5a227e3b 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -220,11 +220,17 @@ def parse_entity_models(models) required << property_name.to_s if p.delete(:required) + type = if p[:type] + p.delete(:type) + elsif (entity = model.exposures[property_name][:using]) + parse_entity_name(entity) + end + if p.delete(:is_array) - p[:items] = generate_typeref(p[:type]) + p[:items] = generate_typeref(type) p[:type] = 'array' else - p.merge! generate_typeref(p.delete(:type)) + p.merge! generate_typeref(type) end # rename Grape Entity's "desc" to "description" diff --git a/spec/response_model_spec.rb b/spec/response_model_spec.rb index ceb80491..6efc2082 100644 --- a/spec/response_model_spec.rb +++ b/spec/response_model_spec.rb @@ -11,6 +11,9 @@ 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 :kind2, using: Kind, documentation: { desc: 'Secondary kind.' } + expose :kind3, using: 'MyAPI::Entities::Kind', documentation: { desc: 'Tertiary kind.' } + expose :tags, using: 'MyAPI::Entities::Tag', documentation: { desc: 'Tags.', is_array: true } expose :relation, using: 'MyAPI::Entities::Relation', documentation: { type: 'MyAPI::Relation', desc: 'A related model.' } end @@ -18,6 +21,10 @@ class Relation < Grape::Entity expose :name, documentation: { type: 'string', desc: 'Name' } end + class Tag < Grape::Entity + expose :name, documentation: { type: 'string', desc: 'Name' } + end + class Error < Grape::Entity expose :code, documentation: { type: 'string', desc: 'Error code' } expose :message, documentation: { type: 'string', desc: 'Error message' } @@ -88,6 +95,9 @@ def app 'properties' => { 'text' => { 'type' => 'string', 'description' => 'Content of something.' }, 'kind' => { '$ref' => 'MyAPI::Kind', 'description' => 'The kind of this something.' }, + 'kind2' => { '$ref' => 'MyAPI::Kind', 'description' => 'Secondary kind.' }, + 'kind3' => { '$ref' => 'MyAPI::Kind', 'description' => 'Tertiary kind.' }, + 'tags' => { 'items' => { '$ref' => 'MyAPI::Tag' }, 'type' => 'array', 'description' => 'Tags.' }, 'relation' => { '$ref' => 'MyAPI::Relation', 'description' => 'A related model.' } } )