alba: Can't get root_key to work (without inference)
Describe the bug
:root_key does not seem to be doing what I expect, I can’t get it to have an effect. I am not using inference, but still expect to be able to specify a root key manually.
To Reproduce
class MyThing
attr_accessor :name
def initialize(name:)
@name = name
end
end
class TestSerializer
include Alba::Resource
root_key :widget
attributes :name
end
TestSerializer.new( MyThing.new(name: "joe") ).serializable_hash
# => {:name=>"joe"}
Expected behavior
I expected there to be a root key in the output, like
{ widget: {:name=>"joe"} }
Actual behavior
There is no root key in the output, just {:name=>"joe"}. The root_key seems to be ignored?
This also means I can’t get meta to output, because it requires a recognized root_key.
Additional context
Alba 1.6.0
I know the root_key function is integrated with “inference”, I am not using alba inference, not calling Alba.enable_inference!, not sure if that matters.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (10 by maintainers)
I also think now that
as_jsonis available for Alba. https://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_jsonto_jsonandas_jsonshould behave similarly, so we can keepserializable_hashandto_himplementation as is and implement new behavior inas_json.