-
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
JSON API nicer include syntax #1128
Comments
I can work on this once my json adapter implementation is merged |
I like the idea of a better syntax for |
But why use the string syntax? what does it get us? |
@NullVoxPopuli I would like to get rid of the string syntax but
|
Also, the string syntax makes it easy to sideload a "grand child" resource without sideloading the "child resource", which I don't really see a use case of, but still. |
@beauby we could support both?, esp if the string is gonna get converted to the array/hash syntax anyway. like: [
:author,
comments: {
relationships_only: true,
associations: [:author]
}
] but that is getting kinda verbose... :-( |
I think you all need to stick to one or the other. Supporting both adds unneeded complexity for little benefit. I personally think that if strings are passed in, they should be converted over to a Hash. If a Hash is passed in, it is used as-is. |
@jfelchner The idea is to support an "hybrid" syntax, which is made of Arrays of Symbols and Hashes of Arrays (in terms of grammar, it would be something along the lines of |
Also, whatever does the conversion from the strings to hashes internally needs to be able to be called as a utility method. This way if the user wants to pass in For example if I have an association called |
@beauby agreed 👍 That's what I meant by "Hash". I don't know why you didn't catch on. 😉 😄 |
@jfelchner That seems like an authorization concern I am not sure AMS should be responsible of. Currently you could get away with defining two serializers, one for the Admins, one for the Users, which define different sets of relationships, and only the relationships declared in the serializer can be included. |
@jfelchner If we agree on the format of include, I think you will be pleased by #1131 (although it states that the string syntax is removed, I'm actually adding it back since I overlooked the JSONAPI spec on that one). |
@beauby no no. It definitely shouldn't be responsible for authorization. What I'm saying is something like: class MyController
def index
render json: @project_group,
include: sanitized_includes
end
private
def sanitized_includes
hashified_params = ActiveModel::Serializer.convert_string_includes_to_hash(params[:includes])
ActionController::Parameters.new(hashified_params).permit(:blah, :blah)
end
end Obviously just pseudocode. The general gist is that users of AMS won't have to duplicate the logic of how the |
@jfelchner Well, in that case you will be even more pleased with #1131, since I am currently precisely working on that "translator". |
@beauby Fantastic 😀 |
I believe that But, I think, there is a related but somehow not so popular opposite issue: when the |
@jfelchner, @estum: I just updated #1131 to handle both formats under the hood in the |
@estum the JSON API spec only specifies what the client should send to the server. Which, as you stated is a string of comma separated values. But what happens to it after that point is up to the backend implementation to decide. |
@beauby you are awesome. I can't wait to get all this goodness merged in! |
Nice everyone, indeed we need to keep the string format, ppl have being using it and it looks similiar to what JSON API spec on what clients should send. |
@jfelchner It is merged. The following methods are now available as well:
Can we close this issue? |
Looks awesome! Great work @beauby! |
Nice! 😄 |
I have a lot of nested associations in my app, and was getting tired of writing really long strings/arrays for the include option. So I wrote a little helper that turns an include that looks like this:
into an include value that looks like that
I tried to follow the format of what you can pass in to
ActiveRecord::Relation#includes
method.It's a little messy but something along these lines:
Does this sound helpful to anyone else? I wouldn't mind submitting a PR if it sounds like people are interested.
The text was updated successfully, but these errors were encountered: