-
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
Document serializer/each_serializer options #1191
Comments
👍 for the explanation. As I said in #Icantremember, I think the NoSerializerError for associations should not be rescued, or at least should issue a warning when rescued. |
Actually, that's what it was introduced for, though perhaps Ref:
# 1. Failure to serialize an element in a collection, e.g. [ {hi: "Steve" } ] will fail
# with NoMethodError when the ArraySerializer finds no serializer for the hash { hi: "Steve" },
# and tries to call new on that nil.
# 2. Convert association_value to hash using implicit as_json
# 3. Set as virtual value (serializer is nil)
# 4. Consider warning when this happens
virtual_value = association_value
virtual_value = virtual_value.as_json if virtual_value.respond_to?(:as_json)
association_options[:association_options][:virtual_value] = virtual_value |
It would be great to have this explanation of things in SerializableResource are other related areas. Also, Is there a place currently documented that states that AMS does not serialize primitives? |
Yeah, that's why I made this issue.. would be good to have a overview something like this but that won't break with changes
Dunno. It does come up every now and then. I do think Probably best would be a blurb in the README
|
👍 |
@rails-api/ams reminder to myself etc. to write this up, maybe in an ARCHITECTURE.md? |
from: #1156 (comment)
So, AMS doesn't handle primitives, and when they find their way it, they are passed through as is. ( Specifically, when you're not rendering an object with a defined serializer.)
How options are parsed
serializable_resource = ActiveModel::SerializableResource.new(resource, options)
if serializable_resource.serializer?
serializer?
isuse_adapter? && !!(serializer)
use_adapter?
: 'True when no explicit adapter given, or explicit value is truthy (non-nil); False when explicit adapter is falsy (nil or false)'serializer
::serializer
option, elseActiveModel::Serializer.serializer_for(resource)
serializer
is::serializer
option is removed from the serializer_opts hash:each_serializer
option is present, it is removed from the serializer_opts hash and set as the:serializer
optionserializer_instance = serializer.new(resource, serializer_opts)
adapter_instance = ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)
serializer_instance
was aArraySerializer
and the:serializer
serializer_opts is present, then that serializer is passed into each resource:active_model_serializers/lib/active_model/serializer/array_serializer.rb
Lines 14 to 16 in a54d237
The upshot of which is that
:serializer
option and:each_serializer
is used as the serializer for each resource in the collection:serializer
option is the resource serializerAside, if the collection serializer (ArraySerializer) cannot identify a serializer for a resource in its collection, it raises
NoSerializerError
which is rescued inAcitveModel::Serializer::Reflection#build_association
which sets the association value directly:reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
(which is called by the adapter asserializer.associations(*)
The text was updated successfully, but these errors were encountered: