Skip to content

Commit 2a6a5d0

Browse files
committed
If :type is omitted, see if it's available in :using
1 parent 4a49da2 commit 2a6a5d0

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Next Release
22

3+
* [#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).
34
* Your contribution here.
45

56
### 0.9.0 (December 19, 2014)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ end
229229

230230
### Relationships
231231

232-
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'`.
232+
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`.
233233

234234
#### 1xN
235235

lib/grape-swagger.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,17 @@ def parse_entity_models(models)
220220

221221
required << property_name.to_s if p.delete(:required)
222222

223+
type = if p[:type]
224+
p.delete(:type)
225+
elsif (entity = model.exposures[property_name][:using])
226+
parse_entity_name(entity)
227+
end
228+
223229
if p.delete(:is_array)
224-
p[:items] = generate_typeref(p[:type])
230+
p[:items] = generate_typeref(type)
225231
p[:type] = 'array'
226232
else
227-
p.merge! generate_typeref(p.delete(:type))
233+
p.merge! generate_typeref(type)
228234
end
229235

230236
# rename Grape Entity's "desc" to "description"

spec/response_model_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ 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 :kind2, using: Kind, documentation: {desc: 'Secondary kind.'}
15+
expose :kind3, using: 'MyAPI::Entities::Kind', documentation: {desc: 'Tertiary kind.'}
16+
expose :tags, using: 'MyAPI::Entities::Tag', documentation: {desc: 'Tags.', is_array: true }
1417
expose :relation, using: 'MyAPI::Entities::Relation', documentation: { type: 'MyAPI::Relation', desc: 'A related model.' }
1518
end
1619

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

24+
class Tag < Grape::Entity
25+
expose :name, documentation: { type: 'string', desc: 'Name' }
26+
end
27+
2128
class Error < Grape::Entity
2229
expose :code, documentation: { type: 'string', desc: 'Error code' }
2330
expose :message, documentation: { type: 'string', desc: 'Error message' }
@@ -88,6 +95,9 @@ def app
8895
'properties' => {
8996
'text' => { 'type' => 'string', 'description' => 'Content of something.' },
9097
'kind' => { '$ref' => 'MyAPI::Kind', 'description' => 'The kind of this something.' },
98+
'kind2' => { '$ref' => 'MyAPI::Kind', 'description' => 'Secondary kind.' },
99+
'kind3' => { '$ref' => 'MyAPI::Kind', 'description' => 'Tertiary kind.' },
100+
'tags' => { 'items' => {'$ref' => 'MyAPI::Tag'}, 'type' => 'array', 'description' => 'Tags.' },
91101
'relation' => { '$ref' => 'MyAPI::Relation', 'description' => 'A related model.' }
92102
}
93103
)

0 commit comments

Comments
 (0)