-
Notifications
You must be signed in to change notification settings - Fork 1.4k
WIP - Deserializer implementation #950
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
WIP - Deserializer implementation #950
Conversation
af2dc47
to
9b9177a
Compare
Would it make sense to include |
👍 Some thoughts I have:
How will this work with PORO objects, presenter objects that accept parameters for multiple underlying objects as part of a renormalization of how the underlying data is stored vs presented? |
I guess if we're going to add another sort of transformation, I'd really like to be able to generate the serializer/deserializer from a json_schema such what a hypermedia client library like heroics or translator such as swagger do. And/or maybe use transformation 'service' object such as https://github.com/solnic/transproc |
9b9177a
to
b4c7911
Compare
767d5ba
to
f15071b
Compare
Hey ppl, just letting you know I've being working on this. I've just updated the initial PR. You might notice that the implementation changed a little bit. We've being improving it with Rails Team. 😄 @saneshark thanks for share you thoughts, indeed we will be able to help user into sanitizing their data too with this. Maybe in next steps. For now, deserialization would still work with PORO project because it just return and instance of the object. @bf4 Indeed, as I said, deserialization will enable us to do a lot more, new features that will make it even easier to write APIs, I don't think the PR will handle those features right now, but some of those might follow this one, once finished and merged. btw this is still WIP 😄 cc @spastorino |
8f90729
to
10141e5
Compare
bbae1fe
to
7448e2a
Compare
It took longer than I expected but this is rebased, finally. |
class ExplicitSerializerTest < ActionController::TestCase | ||
class ExplicitSerializerTestController < ActionController::Base | ||
def render_using_explicit_serializer | ||
@profile = Profile.new(name: 'Name 1', | ||
description: 'Description 1', | ||
comments: 'Comments 1') | ||
render json: @profile, serializer: ProfilePreviewSerializer | ||
render json: @profile, serializer: ProfilePreviewSerializer |
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.
Any reason for this extra space?
Fix previous merge.
Fix extra spaces.
Minor code improvement.
Revert Serialization renaming.
Namespace AR models to prevent conflict with POROs.
It seems to work for me. Thanks for your great job! |
So, what's the status on this one? |
Yeah, same question here, when will the deserializer implementation for the JSON API adapter be merged into the master? Maybe it should be merged before implementation for the JSON adapter is ready – otherwise we'll need to wait even longer until we can use this on the official gem. Also I suggest to include some usage to the README. |
I have also just read a lot more into this and tried to get deserialization to work on my real-world app that I'm developing right now – but there's at least two things that don't work yet:
Although the last point isn't necessarily a part of the JSON API specification (as far as I could see) there still needs to be a solution on how to deal with bidirectional presence validations like this: A You can check out the entire project for a working example here, but I'm quoting the parts in question below, too, too keep things clean. When trying to fix this it's probably easiest though to just checkout the projects Here's my Serializers: ##
# Describes serialization of a User to a JSON format and vice versa according to the JSON API specification.
class UserSerializer < ActiveModel::Serializer
attributes :id, :first_name, :last_name, :nickname, :email, :gender, :birthday, :country_code, :image
has_many :spoken_languages
end
##
# Describes serialization of a SpokenLanguage to a JSON format and vice versa according to the JSON API specification.
class SpokenLanguageSerializer < ActiveModel::Serializer
attributes :id, :language_code
belongs_to :user
end Here's my Controller: ##
# The API V1 controller managing users, logins/logouts, registrations, password resets and the like.
class Api::V1::UsersController < Api::V1Controller
# Creates a new user with given parameters and renders a JSON response.
#
# @return [void]
def create
@user = User.new(user_params)
if @user.save
render json: @user, status: :created, location: @user
else
render json: { errors: @user.errors }, status: :unprocessable_entity
end
end
private
# Deserializes the users parameters into ActiveRecord accessable format and filters unpermitted values.
#
# @return [Hash] permitted attributes for a user
def user_params
UserSerializer.deserialize(params, [:password])
end
end And here's my spec: it 'responds with created including data for valid data' do
post :create, {
data: {
type: 'users',
attributes: attributes_for(:user), # using FactoryGirl, factories are working! (ling-approved)
relationships: {
spoken_languages: { data: [{ type: 'spoken_languages', attributes: attributes_for(:spoken_language) }] }
}
}
}
expect(response).to have_http_status(:created)
# more expectations
end |
@Dschee Thanks for the thoughts you put into this one.
and
Therefore, a related object should not be built. Only existing objects should be supplied. In your case, you could either change your API to comply to the spec, or build a custom adapter based on the JSONAPI one. |
Not sure what's the status here as of now, so I just wanted to let those like me who were looking for AMS-y deserializing about an alternative Deserializer gem. It pretty much follows the API conventions of AMS. Hope it helps others and it might even benefit the core devs in picking/not picking an approach, API, etc. |
Thanks for the heads up @sahal2080! |
Should we close this now that #1248 has been merged? |
@kurko we implemented a new one, that was merged already and will be on RC4 soon 😄 |
This link is now here Deserializer |
We decided that it's time to work with deserialization. 🎉🎉 🎉
AMS aim to bring convention over configuration to JSON generation, but as it evolves into a more complete solution to build great APIs it makes sense to also handle deserialization.
The initial proposal of Deserialization is to convert your json input into something the
ActiveRecord
understands.It will play really nice with json-api, enabling us to follow it conventions when creating and updating objects using AMS.
How it works:
attributes
method inside an existing serializerdeserialize :title, :body
)Usage:
post_serialization.rb
post_controller.rb
This is a Working In Progress yet 😝