hanami/utils

exposed models in result of Interactor#call become Hanami::Utils::Hash

repomaa opened this issue · 9 comments

exposing models in an interactor results in them being transformed into a Hanami::Utils::Hash. I'd expect them to stay instances of their model class.

@AlfonsoUceda You said you were going to take a look at this on Gitter. If you haven't started it yet I can have a look. I have some spare time tonight that I would like to fill in with something :)

@gizotti sorry but I couldn't do it ;) take a look if you can, I'll try this evening.

@AlfonsoUceda no worries man. I just didn't want to step on your toes.

I'll have a look at it

This behaviour is because inside the interactor we use Hanami::Utils::Hash and in the initializer has #to_hash. Hanami::Entity implement this method.

I saw this yesterday, just didn't have time to update here, sorry.

Does that mean that Interactor is behaving as expected and we need to work around it? Or should we add a case on the initializer so it keeps the Model in its original form?

@AlfonsoUceda The problem might be even deeper than the Hanami::Utils::Hash usage.

Here's my Interactor class:

require 'hanami/interactor'

class Test
  include Hanami::Interactor
  expose :post, :params

  def initialize(params)
    @params = params
    @post   = Post.new(@params)
  end

  def call
    @post = PostRepository.new.create(@post)
  end
end

This is the payload passed to the _payload method:

{:post=>#<Post:0x007fee7f758c10 @attributes={:id=>28, :title=>"Title", :body=>"BODY", :created_at=>2017-01-31 22:51:14 UTC, :updated_at=>2017-01-31 22:51:14 UTC}>, :params=>{:title=>"Title", :body=>"BODY"}}

And the result of Utils::Hash.new(payload).symbolize! which is the same as above:

{:post=>#<Post:0x007fee7f758c10 @attributes={:id=>28, :title=>"Title", :body=>"BODY", :created_at=>2017-01-31 22:51:14 UTC, :updated_at=>2017-01-31 22:51:14 UTC}>, :params=>{:title=>"Title", :body=>"BODY"}}

However, this is what a I get from result.post:

[3] pry(main)> r.post
=> {:id=>29, :title=>"Title", :body=>"BODY", :created_at=>2017-01-31 22:57:08 UTC, :updated_at=>2017-01-31 22:57:08 UTC}

I think im not even close to getting this one. But hopefully my data helps us.

@gizotti Thanks for getting back. Sorry for the misunderstanding, but we consider it a bug. @AlfonsoUceda and @mereghost are looking into it. The fix should be easy: avoid to use Utils::Hash for the payload.

@jodosha @AlfonsoUceda Isn't this fixed by the changes in the PR #162?
I think the previous version of symbolize! was the cause of this (see: https://github.com/hanami/utils/pull/162/files#diff-bfa5aa835d37e08f696e6c62759f2f3dL68)

Fixed by #190

@gizotti @jreinert @alexd16 the problem is fixed ;)