Skip to content

Commit 86ed8df

Browse files
committed
Merge pull request #171 from jhollinger/explain-how-type-works
Explain how the documentaiton type option is matched to class names. Fix...
2 parents 37a1b7e + a8fbb9b commit 86ed8df

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ end
227227

228228
### Relationships
229229

230+
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'`.
231+
230232
#### 1xN
231233
232234
```ruby
@@ -235,7 +237,7 @@ module API
235237
class Client < Grape::Entity
236238
expose :name, documentation: { type: 'string', desc: 'Name' }
237239
expose :addresses, using: Entities::Address,
238-
documentation: { type: 'Address', desc: 'Addresses.', param_type: 'body', is_array: true }
240+
documentation: { type: 'API::Address', desc: 'Addresses.', param_type: 'body', is_array: true }
239241
end
240242
241243
class Address < Grape::Entity
@@ -266,7 +268,7 @@ module API
266268
class Client < Grape::Entity
267269
expose :name, documentation: { type: 'string', desc: 'Name' }
268270
expose :address, using: Entities::Address,
269-
documentation: { type: 'Address', desc: 'Addresses.', param_type: 'body', is_array: false }
271+
documentation: { type: 'API::Address', desc: 'Addresses.', param_type: 'body', is_array: false }
270272
end
271273
272274
class Address < Grape::Entity

spec/response_model_spec.rb

+28-11
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
before :all do
55
module MyAPI
66
module Entities
7-
class BaseEntity < Grape::Entity
8-
def self.entity_name
9-
name.sub(/^MyAPI::Entities::/, '')
10-
end
7+
class Kind < Grape::Entity
8+
expose :title, documentation: { type: 'string', desc: 'Title of the kind.' }
119
end
1210

13-
class Something < BaseEntity
11+
class Something < Grape::Entity
1412
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
13+
expose :kind, using: Kind, documentation: { type: 'MyAPI::Kind', desc: 'The kind of this something.' }
1514
end
1615

17-
class Error < BaseEntity
16+
class Error < Grape::Entity
1817
expose :code, documentation: { type: 'string', desc: 'Error code' }
1918
expose :message, documentation: { type: 'string', desc: 'Error message' }
2019
end
@@ -59,22 +58,40 @@ def app
5958
{
6059
'code' => 200,
6160
'message' => 'OK',
62-
'responseModel' => 'Something'
61+
'responseModel' => 'MyAPI::Something'
6362
},
6463
{
6564
'code' => 403,
6665
'message' => 'Refused to return something',
67-
'responseModel' => 'Error'
66+
'responseModel' => 'MyAPI::Error'
6867
}
6968
]
7069
)
71-
expect(subject['models'].keys).to include 'Error'
72-
expect(subject['models']['Error']).to eq(
73-
'id' => 'Error',
70+
71+
expect(subject['models'].keys).to include 'MyAPI::Error'
72+
expect(subject['models']['MyAPI::Error']).to eq(
73+
'id' => 'MyAPI::Error',
7474
'properties' => {
7575
'code' => { 'type' => 'string', 'description' => 'Error code' },
7676
'message' => { 'type' => 'string', 'description' => 'Error message' }
7777
}
7878
)
79+
80+
expect(subject['models'].keys).to include 'MyAPI::Something'
81+
expect(subject['models']['MyAPI::Something']).to eq(
82+
'id' => 'MyAPI::Something',
83+
'properties' => {
84+
'text' => { 'type' => 'string', 'description' => 'Content of something.' },
85+
'kind' => { '$ref' => 'MyAPI::Kind', 'description' => 'The kind of this something.' }
86+
}
87+
)
88+
89+
expect(subject['models'].keys).to include 'MyAPI::Kind'
90+
expect(subject['models']['MyAPI::Kind']).to eq(
91+
'id' => 'MyAPI::Kind',
92+
'properties' => {
93+
'title' => { 'type' => 'string', 'description' => 'Title of the kind.' }
94+
}
95+
)
7996
end
8097
end

0 commit comments

Comments
 (0)