-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
prevent ActiveModel::Serializer::Fieldset.fields_for crashing in case… #2353
Conversation
… json_key is a string
@wasifhossain should not it be part of a new tag ? fetching the v0.10.10 will not provide the fix 🤔 We're using dependabot which try to retrieve the newest tag. |
@bf4 can you please suggest the best step that we can take here. (#2343 (comment) as a reference) thanks |
@wasifhossain 0.10.11 is fine as is 0.10.10.1, which is a thing. I don't have a strong feeling |
@bf4 @wasifhossain can we settle on a tag so the update is available ? Since its a fix I would go for |
I would also prefer to tag it as 0.10.10.1, but the fact is, unless we create a new branch from another point that I would like to address before we make the release is #2350 (comment). otherwise we will continue to receive relevant issues
also we received a good number of issues most recently resulted from lib/active_model/serializer.rb#L387, which I think is due to lack of guide that tells about the requirement on how to define the PORO. docs/howto/serialize_poro.md explains it nicely, but the linter only mentions that its required if caching is enabled. I think we should highlight the point for the PORO that it should respond to @bf4 any thoughts, please? |
|
regarding item 2 above:
right now, rather we can set an instance of still that would make the |
i agree that we can work on a PR to make the |
Nice! Let me know if I can be of anyway help. |
@aovertus thanks for your hands! btw I need to take a dive into the internals and lets see what I can come up with in next few days. |
I think a first-level fix could be - instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))
+ if (fields_options = instance_options.delete(:fields)))
+ instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(fields_options))
+ end so that it's only even possible to get a fieldset when there are field options present secondly, we need to think through how a fieldset in should even work without a json_key. My feeling is, you can't have your cake and eat it too. If you want to use a fieldset, you need a 'json_key'. I think if we make the above change, then we can raise a runtime error changing if (fieldset = adapter_options[:fieldset])
options[:fields] = fieldset.fields_for(json_key)
end to if (fieldset = adapter_options[:fieldset])
options[:fields] = fields_for!(fieldset)
end
.....
def fields_for!(fieldset)
fieldset.fields_for(fieldset_type)
end
def fieldset_type
json_key
rescue NoMethodError
raise NoMethodError, "Cannot determine fieldset type for serializer instance class=#{self.class.name} object=#{object.class.name}"
end |
😄 |
We were burned by this as well when we tried to upgrade. In case anyone ends up here because there's no new version out yet, here's the fix I'm using locally in an initializer: ActiveModel::Serializer::Fieldset.prepend(
Module.new do
def fields_for(type)
super(type.to_s)
end
end
) |
Purpose
Current AMS implementation allows things like this:
render json: users, each_serializer: UserSerializer, root: :users
but the new fieldset class don't accept anything else than string for
#fields_for
Changes
Allow
#fields_for
to receive a symbol and parse it to string before any manipulationvalue is coming from
#json_key
methodCaveats
Related GitHub issues
Additional helpful information