hanami / utils

Ruby core extentions and class utilities for Hanami

Home Page:http://hanamirb.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Interactor result modifying the exposure values

rpanachi opened this issue · comments

After updated to hanami-utils v0.9, all interactors in my project are broken, because the value in the exposures are being changed by Hanami::Utils::Hash#symbolize! method. Example:

class MyInteractor
  include Hanami::Interactor
  exposure :user
  def call
    @user = UserModel.find(params[:user_id)
  end
end

Using hanami-utils 0.8:

result = MyInteractor.new(user_id: 1).call
result.user # <UserModel: id: 1, name: "User Name">
result.user.class # UserModel

After update to hanami-utils 0.9.0:

result = MyInteractor.new(user_id: 1).call
result.user # {id: 1, name: "User Name"}
result.user.class # Hanami::Utils::Hash

PS: Yes, my UserModel class implements to_hash method.

This behavior of Interactor's result was introduced by 6243607 and 8e4c78d commits. I didn't find any reference to this change in the Hanami "Upgrade Notes" to v0.9.0 or official guide.

I fixed the problem by monkey patching Hanami's Hash to the v0.8 version. Please consider to revert back this change or officially document this behavior on guides. Thanks!

Thanks for opening this. Sorry about things breaking for you. We're moving fast (and breaking things 😞) to try out different things before 1.0, when we'll have to make a major version bump to break backward compatibility, since we follow Semantic Versioning.

Why does UserModel implement to_hash? Could it implement to_h instead?

I think to_hash makes sense for something like form submission parameters coming in to a server, which can essentially "act like" a hash in every way. I'm not sure UserModel has that same relationship to Ruby's Hash class.

@cllns actually my UserModel is a Sequel::Model, so it implements to_hash by inheritance: https://github.com/jeremyevans/sequel/blob/master/lib/sequel/model/base.rb#L1288

But my point here is that Hanami::Utils::Hash are changing the Interactor behavior by changing the values in the exposures (calling to_hash on each element).

I understand that you have to move fast, but break this kind of things is counter-productive. It took about 2 hours of debug to figure out that this "wierd" behavior on Interactor are caused by Hanami's Hash. And the only way to fix that, right now, is monkey patching to the old version.

Imho, if this behavior intends to cover things like form submission and things that actually can be safetelly converted to_hash (like a Hash instance), I suggest to use a different approach or even a specialized class for that.

Hi @rpanachi thanks for opening the issue, as @cllns said we are under v1.0 and we can break things, sorry for the inconveniences. In my opinion this is a bug, because if you expose that instance variable as UserModel, it shouldn't be changed to Hanami::Utils::Hash.

cc @jodosha

@rpanachi I'm terribly sorry about your problem. We already have a solution for this, but we can't merge it straight away because that requires another breaking change for Utils::Hash#symbolize!.

We plan to ship this fix in a couple of weeks.

Ok, I'll wait for the definitive solution. Thanks @jodosha for the feedback

Fixed by #162