Skip to content
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

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

Open
23tux opened this issue Jun 8, 2021 · 1 comment

Comments

@23tux
Copy link
Contributor

23tux commented Jun 8, 2021

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.

@DmitryTsepelev
Copy link
Owner

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 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants