-
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
Preserve the serializer type when fragment caching #1458
Preserve the serializer type when fragment caching #1458
Conversation
We were not previously cloning the type setting into the dynamically generated cached/non-cached serializers for a given fragment-cached serializer. This led to the type generated for JsonApi having the wrong value when fragment caching is enabled by adding either :except or :only options to cache. This pulls the type setting from the fragment-cached serializer forward onto the dynamic caching classes so it is preserved in the output.
The way CachedSerializer#fragment_cached? worked previously would return true even in an environment where caching was disabled as defined by `ActiveModelSerializers.config.perform_caching`. Added check for `_cache` like in the `cached?` method before checking whether `_cache_only` or `_cache_except` is set.
@@ -24,7 +24,7 @@ def cached? | |||
end | |||
|
|||
def fragment_cached? | |||
@klass._cache_only && !@klass._cache_except || !@klass._cache_only && @klass._cache_except | |||
@klass._cache && (@klass._cache_only && !@klass._cache_except || !@klass._cache_only && @klass._cache_except) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be it's own PR. would you mind? TYMessaging@e9e2b57
Awesome, thanks! 👍 some comments above |
def test_fragment_fetch_with_type_override | ||
type_serializer = TypedRoleSerializer.new(Role.new(name: 'Another Author')) | ||
type_hash = ActiveModel::Serializer::Adapter::JsonApi.new(type_serializer).serializable_hash | ||
assert_equal(type_hash[:data][:type], TypedRoleSerializer._type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to use the serializable test helper (below may contain typos)
serialization = serializable(Role.new(name: 'Another Author'), serializer: TypedRoleSerializer, adapter: :json_api).serializable_hash
assert_equal(TypedRoleSerializer._type, serialization.fetch(:data).fetch(:type))
@bf4 comments addressed. Will open a secondary PR with the fix for |
Closed by 527c2ae |
Merged in a409982 |
This was merged, but GitHub didn't pick it up. I'm not sure why |
We were not previously cloning the type setting into the dynamically
generated cached/non-cached serializers for a given fragment-cached
serializer. This led to the type generated for JsonApi having the wrong
value when fragment caching is enabled by adding either :except or :only
options to cache.
This pulls the type setting from the fragment-cached serializer forward
onto the dynamic caching classes so it is preserved in the output.