Skip to content
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

If :type is omitted, see if it's available in :using #196

Merged
merged 1 commit into from
Jan 17, 2015
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
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions spec/response_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ 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

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' }
Expand Down Expand Up @@ -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.' }
}
)
Expand Down