DmitryTsepelev/store_model

Handling of unknown attributes in StoreModel.one_of converts keys to symbols

23tux opened this issue · 1 comments

23tux commented

It seems like when handling unknown attributes for a StoreModel.one_of type, the keys of the backend get converted into symbols. See this example class:

class RestConfig
  include StoreModel::Model

  attribute :type, :string
  attribute :key, :string
end

class Service
  include StoreModel::Model

  Config = StoreModel.one_of do |json|
    # json == {:type=>"RestConfig"}
    json.fetch("type").constantize
  end

  attribute :config, Config.to_type
end

When I pass a backend that has a wrong key, the json.fetch("type") doesn't find it's key anymore, because the keys are somehow converted into symbols {:type=>"RestConfig"}:

pry(main) > Service.new("config" => { "type" => "RestConfig", "keyy" => "ABC" }).inspect

KeyError: key not found: "type"
Did you mean?  :type
from (pry):47:in `fetch'
Caused by ActiveModel::UnknownAttributeError: unknown attribute 'keyy' for RestConfig.
from /usr/local/bundle/gems/activemodel-5.2.6/lib/active_model/attribute_assignment.rb:53:in `_assign_attribute'

Can you tell me, why the keys are converted into symbols? My current fix is to fetch them by string OR symbol. We use the OneOf approach in more and more, so this becomes tedious and buggy over time as not every developer thinks of this edge case.

Hi @23tux!

It was here from the early beginning of Unknown Attributes feature. The reason was that we need to work with keys in the same manner, so I convert all keys to symbols. I guess it will be safe enough to use string everywhere, so PR is welcome 🙂