-
-
Notifications
You must be signed in to change notification settings - Fork 152
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
Entity to_h - nested attribute entity is not converted to hash #483
Comments
Duplicate of #470. You noticed you can use But we didn't really answer the question why this doesn't happen by default though. I agree it's surprising that it doesn't work that way. Any input @mereghost? I wonder if a workaround could be adding an optional param, like |
I vaguely remember a past issue of this up ahead in the framework. Also that's how ruby's We could provide a way to make sure that the object is deep serialized into a hash for convenience, but that's already provided by |
Thanks @cllns it seems I searched not very good before creating an issue.
My potential use case to have recursive to_h serialization was in adopting Entity in Trailblazer's Operation. Input of the typical Operation is Hash of params from Action. Later on, an Entity is retrieved based on params and data from Entity is being manipulated during next steps of the Operation. Idea was to use Entity as DTO through these steps, so each step can modify Entity attribute (and nested also). But at the last step of the Operation I want to persist this Entity to DB via Repository. So I was searching a way to get back a Hash representation of "complex" Entity. P/S Actually my another and wider question first time I tried Hanami was "Oh great, I can get Entity as a "select" query result from Repo, but wait... why I can not put ready made Entity object as a data |
@iJackUA I'm sorry for the veeery late response. We really need to up our game on the documentation side of things (contributions are always welcome ;)). About the insert/update part: if you use the Think of it as a poor man's changeset (which is a concept we might introduce in the future). You pass in what you want to update and we do it. |
Is it by design? Because I was expecting it to work a bit different.
Suppose I have two Entities
I can initialize it that way
now when I do
user.to_h
User entity is serialized to hash, but enclosed UserAuthData is still an object, we did not return to the same
user_params
The only way I see to do that is
Hanami::Utils::Hash.new(user).to_hash
or
Hanami::Utils::Hash.deep_serialize(user)
these methods return
{:id=>3, :auth_data=>{:hi=>"hello"}}
as expectedMaybe Entity object by itself need to have a helper to serialize itself to real nested hash?
Actually I would expect from
entity_object.to_h
to do this by default, but maybe I miss some consideration.The text was updated successfully, but these errors were encountered: