-
Notifications
You must be signed in to change notification settings - Fork 152
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
How to pass :type options when using expose to map to another Grape Entity #82
Comments
Hmm, if I using shop entity like this present Shop.find(params[:id]), with: API::Entity::Shop, type: :full Then the shop's owner field is automatically display as a User entity in full. Is is the expected behaviour? What if I only want to present the shop entity in full only? |
You have in you'r Shop entity also fields exposed with And now you see all fields of Shop and all fields of User? I think that this is the expected |
@linhmtran168 Just ran into this myself. This is working for me (so far). entities/alert.rb
entities/city.rb
endpoints/alerts.rb
|
@linhmtran168 I am closing this, please feel free to bring it up on the Grape mailing list if none of the solutions pan out. |
I think this should be reopened, it's a missing feature. Another user case that would not be possible because of this missing feature is referencing other objects of the same entity. Since we cannot attribute variables, we'd fall in a circular trap. |
@leods92 Can you open a new issue for each of these, please? |
For anyone coming here from Google, I have a different work-around for this, using the OO approach, as hinted in the README (i.e I wanted 3 slightly different versions, when
It would be great if I could just do : class API::Entities::Shop < API::Entities::Base
expose :owner, using: API::Entity::User, type: :base
end But I can't, so I used OO to define each use-case, which is arguably better.
class API::Entities::User < API::Entities::Base
expose :id
expose :username
end
class API::Entities::UserIndex < API::Entities::User
expose :billing_address
end
class API::Entities::UserShow < API::Entities::UserIndex
expose :shipping_addresses
end Then I can use them like so: class API::Entities::Shop < API::Entities::Base
expose :owner, using: API::Entity::User # base version
end Then define the API and use class API::V1::Shop < API::V1::Base
get '/' do
present Shop.all, with: API::Entities::Shop # returns user.id, user.username
end
end class API::V1::User < API::V1::Base
get '/' do
present Shop.all, with: API::Entities::UserIndex # returns user.id, user.username, user.billing_address
end
params do
requires :id, type: Integer
end
get ':id' do
present User.find(params[:id]), with: API::Entities::UserShow # returns user.id, user.username, user.billing_address, user.shipping_addresses
end
end |
I have a User Entity like this
And a Shop Entity
I cannot find a way to pass the
{ type: :full }
option toexpose :owner, using: API::Entity::User
. Without that option, the shop entity will only include user entity with fields without{ type: :full }
.The text was updated successfully, but these errors were encountered: