Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Entity to_h - nested attribute entity is not converted to hash #483

Description

@iJackUA

Is it by design? Because I was expecting it to work a bit different.

Suppose I have two Entities

class User < Hanami::Entity
  attributes do
    attribute :id, Types::Int
    attribute :auth_data, Types::Entity(::UserAuthData)
  end
end
class UserAuthData < Hanami::Entity
  attributes do
    attribute :hi, Types::String
  end
end

I can initialize it that way

user_params = {id: 3, auth_data: {hi: "hello"}}
user = User.new(user_params)

now when I do user.to_h

{:id=>3,
 :auth_data=>#<UserAuthData:0x000055d8e7fcf600 @attributes={:hi=>"hello"}>}

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 expected

Maybe 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions