Skip to content

Explain how the documentaiton type option is matched to class names. Fix... #171

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 2 commits into from
Dec 14, 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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ 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'`.

#### 1xN

```ruby
Expand All @@ -235,7 +237,7 @@ module API
class Client < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
expose :addresses, using: Entities::Address,
documentation: { type: 'Address', desc: 'Addresses.', param_type: 'body', is_array: true }
documentation: { type: 'API::Address', desc: 'Addresses.', param_type: 'body', is_array: true }
end

class Address < Grape::Entity
Expand Down Expand Up @@ -266,7 +268,7 @@ module API
class Client < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
expose :address, using: Entities::Address,
documentation: { type: 'Address', desc: 'Addresses.', param_type: 'body', is_array: false }
documentation: { type: 'API::Address', desc: 'Addresses.', param_type: 'body', is_array: false }
end

class Address < Grape::Entity
Expand Down
39 changes: 28 additions & 11 deletions spec/response_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
before :all do
module MyAPI
module Entities
class BaseEntity < Grape::Entity
def self.entity_name
name.sub(/^MyAPI::Entities::/, '')
end
class Kind < Grape::Entity
expose :title, documentation: { type: 'string', desc: 'Title of the kind.' }
end

class Something < BaseEntity
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.' }
end

class Error < BaseEntity
class Error < Grape::Entity
expose :code, documentation: { type: 'string', desc: 'Error code' }
expose :message, documentation: { type: 'string', desc: 'Error message' }
end
Expand Down Expand Up @@ -59,22 +58,40 @@ def app
{
'code' => 200,
'message' => 'OK',
'responseModel' => 'Something'
'responseModel' => 'MyAPI::Something'
},
{
'code' => 403,
'message' => 'Refused to return something',
'responseModel' => 'Error'
'responseModel' => 'MyAPI::Error'
}
]
)
expect(subject['models'].keys).to include 'Error'
expect(subject['models']['Error']).to eq(
'id' => 'Error',

expect(subject['models'].keys).to include 'MyAPI::Error'
expect(subject['models']['MyAPI::Error']).to eq(
'id' => 'MyAPI::Error',
'properties' => {
'code' => { 'type' => 'string', 'description' => 'Error code' },
'message' => { 'type' => 'string', 'description' => 'Error message' }
}
)

expect(subject['models'].keys).to include 'MyAPI::Something'
expect(subject['models']['MyAPI::Something']).to eq(
'id' => 'MyAPI::Something',
'properties' => {
'text' => { 'type' => 'string', 'description' => 'Content of something.' },
'kind' => { '$ref' => 'MyAPI::Kind', 'description' => 'The kind of this something.' }
}
)

expect(subject['models'].keys).to include 'MyAPI::Kind'
expect(subject['models']['MyAPI::Kind']).to eq(
'id' => 'MyAPI::Kind',
'properties' => {
'title' => { 'type' => 'string', 'description' => 'Title of the kind.' }
}
)
end
end