-
Notifications
You must be signed in to change notification settings - Fork 148
Improvements to include
#60
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
base: master
Are you sure you want to change the base?
Improvements to include
#60
Conversation
|
@christophersansone thank you for working on this. I have only one concern here, please help me understand why do we need to support 3 types of syntaxes for Code-wise, may I suggest two things please:
|
|
@stas Thanks for the quick feedback.
Sorry, where is this? I added a whole new file of specs to define the behavior, and I did not see anything in the Readme. I'd be happy to adjust wherever that is.
I don't think people will do it that way. Just opt for a different syntax, most simply
The existing format is really not very Rubyesque. I don't know of any other library that uses dot notation strings to define nesting. It is also extremely cumbersome and error prone when you are including several relationships several levels deep. Two examples of heavily used Ruby methods that use similar syntax to what I'm proposing is ActiveModel joins and includes, and also ActionController's strong parameters. The syntax will be familiar to all who use these. In fact, I would think that in most cases where people are using ActiveModel and FastJSONAPI, their ActiveModel Of course, we need to keep the dot notation syntax around for backward compatibility, so like it or not, it's not going anywhere. I much prefer the new syntax and think it's more intuitive for the Ruby community. |
@christophersansone I'm looking at this https://jsonapi.org/format/#fetching-includes Sorry, I'm just trying to keep things as close as possible to what the spec suggests. |
|
@stas Ahh, okay. Well, it also says it needs to be a comma-separated, dot notated list, all in one string, e.g. I think the main difference here is that the json:api spec is (rightfully) language agnostic. But this library lives in Ruby and is used by Ruby developers. Sometimes, for some projects, the list of includes will come from the |
|
@christophersansone let's just keep the API interface we already have in place. I can't imagine folks parsing a query string of comma-separated dot-notation value into a hash, so we're definitely not going in the right direction by supporting that. The most common use-case here is that one sends a query param like in the spec examples, at most that is split into a list of values and fed to Thanks! |
Fixes Problems with the validation of the
includeoption #41. Originally,includewould be validated up front every time a serializer is instantiated, which (a) takes time, and (b) is limited to only validating static relationships, so polymorphic, object blocks, serializer blocks, etc. could not be validated. Now, instead, it will simply assume thatincludeis valid, attempt the serialization, and fail if the relationship does not exist.Per @stas's comment in Problems with the validation of the
includeoption #41, the validation raises a custom exception instead of a plain oldArgumentError. I created anerrors.rbfile so that we can have a common place to define custom exceptions, like most gems do.Improved the syntax of
includeto be much more flexible. Now you can specify strings, symbols, arrays, and hashes (for nested relationships). Dot notation still works, and in fact is improved because now you don't need to specify the parents separately, e.g.[ :actors, :'actors.agency' ]. Now you can simply say'actors.agency', and it implicitly includesactorstoo. But best of all, you can now write this as{ actors: [ :agency ] }, or simply{ actors: :agency }. In general, this should also be a performance improvement, because the code originally parsedincludeand split strings during each iteration. Now it parses it once up front, builds a nice relationship tree, and passes the proper branch of the tree to each relationship during serialization.