-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Bringing Filter to 0.10.x #1058
Comments
This is also a feature of the JSON API |
But in specific to JSON-API I think the |
Not sure I understand. Fields is different. |
Yup, you're right I messed up. It seems to me that some cases are not so useful, taking into considerations the rails context we usually have, like: But as I said, is part of the convention and we should aim for that. 😄 |
Add a method named filter that by default returns all fields. Actual serialised implementations can override this method to dynamically filter the list of attributes to be serialized. Implements rails-api#1058.
Add a method named filter that by default returns all fields. Actual serialised implementations can override this method to dynamically filter the list of attributes to be serialized. Implements rails-api#1058.
Add a method named filter that by default returns all fields. Actual serialised implementations can override this method to dynamically filter the list of attributes to be serialized. Implements rails-api#1058.
I was just working on this, this was my implementation: class Api::BaseController
protected
def index(collection = [])
render json: collection.where(filter_params).paginate(page_params), include: params[:include]
end
private
def filter_params
params.fetch(:filter, {}).permit(filter_attributes << :id).transform_values { |v| v.split(',') }
end
def filter_attributes
serializer = "Spree::#{controller_name.titlecase.singularize}Serializer"
serializer.constantize._attributes
end
def page_params
params.fetch(:page, {}).permit(:number, :size)
end
end
class Api::PricesController
def index
super Price.all
end
def show
super Price.find(params[:id])
end
end Its based on the controller and I think the JSON API's definition of a filter is to identify objects that have an id of
The filtering should be more controller based since we're focusing more on content coming from a URI and deciphering what is and isn't secure to accept. The fields is more of trying to focus on minimizing the JSON payload; especially over making a whitelist that doesn't include certain relationships (like not wanting 50,000 ids back over some requests, but not others). |
Considering what the spec says:
I think it is not AMS's responsibility to handle this parameter. |
However, the 0.8.0 filter feature (which is unrelated to the JSONAPI filter) remains a useful tool that I believe should be available in 0.10.0. |
@beauby considering how easy it is to do on ones own, maybe make another gem that just adds more conventional sugar. |
The 0.8.0 "filter" feature might need to be renamed to "fields". I think that's how the JSON API spec uses the terms. |
Thing is, I'm not even sure what AMS should do with the |
@BenMorganIO The 0.8.0 filter is not really the same as the JSONAPI fields, as the former allows for the dev to pick which attributes/relationships will be serialized depending on certain conditions (like "is the current user an admin?"), whereas the latter allows the client to specify which fields it wishes to receive. |
There's an However, I would opt for AMS to be have a second gem that adds that sugar to ActionController. |
@beauby I like how the filter can do that based on the current users role. I'm 👍 for that. I'd rather opt for something like a (And maybe adding some dynamic helpers for permissions as well. Like if you have Vendor/Buyer users or Designer/Client users.) |
Again, I'm all up for having JSONAPI-related helpers somewhere (I think that'd be great). I just don't think they all belong within AMS. |
@beauby I'm all 👍 for a JSONAPI-related helper gem for AMS. I'd love to put my JSON API |
I am totally against having the filtering logic inside the AMS classes. However there should definitely be an option for filtering attributes (JSONAPI fields param) in a serializer, but outside the AMS class. This was possible in the 0.9.x with the only: option. Maybe relevant to #1141 It's not a good idea to try to make AMS an all around tool. AMS responsibility is to serialize. We should create other gems for filtering (JSONAPI filter param) and including associations (JSONAPI include param) in the db level. Basically AMS 0.10.x should ONLY deal with serializing the objects and not dealing with the incoming params. For the incoming params we should built other gems. Personally I have created 2 gems that are relevant to the JSONAPI filter params which I could make them JSONAPI-compliant if you think it's worth the effort: These gems are supposed to be run before the serializers. Another reason that we shouldn't make AMS an all around tool is because they should be compatible with any db (sql, nosql, redis etc) even with POROs. |
I think I can provide you a gem that filters attributes (JSONAPI fields param) of a resource in the db level for ActiveRecord and Mongoid using AR's select(:attribute1,..) and Mongoid's only(:attribute1,..) respectively. I would create that anyway next week so why not coordinating with you.. |
So, Spree's V2 API actually can filter attributes. What I want to do is focus it on You can see it in action here: http://solidusapi.wildcardlabs.com/#filtering However, what I want it to be able to do is: # current
curl "https://example.com/api/articles?filter[id]=2,3,4"
# hope
curl "https://example.com/api/articles?filter[self.id]=2,3,4"
curl "https://example.com/api/articles?filter[comments.id]=2,3,4" |
@joaomdmoura Can be closed to due merge of #1403 ? Other related issues/prs: |
Yup! #1403 seems to solve it in a better and more elegant way 😁 I'm closing it! |
tl;dr
Older versions of AMS had a
filter
feature that older users are used to. We would like to bring this to 0.10.x.Older versions of AMS had a
filter
feature that older users are used to.We would like to bring this to 0.10.x mainly for two reasons:
The idea is to provide a method named filter, which should return an array used to determine what attributes and associations should be included in the output. This is typically used to customize output based on current_user. For example:
related to issues:
related to PRs:
The text was updated successfully, but these errors were encountered: