-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP] Initial Implementation #1
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
Conversation
Looks like a promising start 👍. I'll post some API suggestions here later. |
So what I had in mind was:
hash = JSONAPI::Rails::DeserializableResource[Post].(document)
class DeserializableCreatePost < JSONAPI::Rails::DeserializableResource[Post]
attribute :foo, required: true
attribute :bar
has_many :baz, types: ['foo', 'bar']
has_one :foobar, required: true
end
hash = DeserializableCreatePost.(params) |
I like this, it's the same/similar pattern as the ralis 5 migrations. for the customized one, I like how that idea could be used in place of strong parameters -- and probably would allow for easier extension if people wanted to get funky with their JSON API create/update requests Also, how do you feel about caching the result of Thanks! |
Re. caching: sure, but that's easy once everything is in place. class DeserializableCreatePost < JSONAPI::Rails::DeserializableResource[Post]
required do
attribute :foo
has_one :foobar
end
optional do
attribute :bar
has_many :baz, types: ['foo', 'bar']
end
end |
I like that alternative DSL, as it more closely matches DeserializableResource from jsonapi/deserializable. Ultimately, it's going to be a subclass either way, so the DSL can go either way (just whatever jsonapi/deserializable allows) |
Yeah, plus it gives a clearer view. |
I'm also pondering the benefits of having |
I'm all for simplification of implementation -- it allows for easier native extensions later. :-) |
So the journey of an incoming payload is:
and |
Yeah for the most part -- I'd said jsonapi-rails would be a drop in replacement for just step 2 -- to keep responsibility separated and such. |
Quick update here: I rewrote
Hence, the DSL here could be as simple as: DeserializablePost = JSONAPI::Rails::DeserializableResource[Post]
DeserializablePost.(params)
# => { ... } which would easily be implemented as a front-end for |
excellent. I looked over your changes, and it does look really easy. I'll make some updates tomorrow morning! |
@@ -0,0 +1,15 @@ | |||
module JSONAPI | |||
require_relative 'jsonapi/rails' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why require_relative
and not require
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was using require_relative everywhere else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That does not really address the question :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to require the whole path jsonapi/etc for every require.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but here this is the whole path. Plus it's not like we require shittons of stuff so I'd be in favor of using require
everywhere.
I think this is getting to a good spot. I had to re-implement some old deserialization to make AMS's existing tests happy. But I'm not sure there is ever going to be a real world use case for the AMS testing scenarios. (where no Model object exists) |
end | ||
end | ||
|
||
attr_accessor :_hash, :_options, :_klass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why accessors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
habit, they'd be fine as attr_reader
s, as they are never written to outside of instantiation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But are they needed? Why not use instance variables?
Some suggestions:
|
Also, we need to find a way to infer the attributes/relationships only if the user does not choose to define them themselves. |
The best answer to the problem mentioned above seems to be no runtime inference, but using generators instead. |
|
After some more thoughts, renaming |
Regarding the generators, I implemented them in #2. |
I think I like
Unrestricted is entirely for AMS -- which if we want to take deserialization testing out of AMS, then it's problem solved (which I would be in huge favor of).
I don't think this would be a problem, because the inferred attributes / relationships only take precedence if a deserializable isn't explicitly used (which would xe explicitly defined entirely, and not have inferred anything)
Nice work! I'll need to give it a test drive |
It is, because if you decide not to allow those attributes anymore, you're toast. |
Regarding AMS and "unrestricted deserialization", I believe it is not an issue to get rid of it as deserialization has always been marked experimental in AMS. |
how do you mean?
excellent |
I mean, if you infer an attribute |
right, I was just saying that to solve the problem, one could inherit from JSONAPI::Deserializable::Resource and build the deserialization however they want |
minor organization updates, spec planning add some tests minor test cleanup add more tests more testing, some cleanup - having AR class caching issues with relationsihps it just returns nil :-/ remove puts railsy result add unrestricted deserializer for backward compatibility remove unrestricted, and rename to JSONAPI::Deserializable::ActiveRecord remove unrestricted, and rename to JSONAPI::Deserializable::ActiveRecord looks like I forgot to add add polymorphic check remove unneeded attr_ settings Add generators for (de)serializable models. (#2) Revert "Add generators for (de)serializable models." (#7)
0344bfb
to
ee140da
Compare
I don't remember the status of this PR :-\ |
Cleaning up my PRs. If this work is still desired, feel free to cherry-pick |
Just working out thoughts in here, trying to figure out API, etc.