Add warnings about global adapter config to docs #2176
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
Clarify effects of global adapter configuration in relation on serializer behavior
Changes
Add warnings to docs
Caveats
No code changes, just documentation
Additional helpful information
This came up for me when trying to test serializer behavior. I wanted to the serialization to include a root key, but despite setting
ActiveModelSerializers.config.adapter = :json
in an initializer, my calls toserializer.as_json
didn't have the root key. When I tried to repro in a fresh Rails app, I found that calls torender
in a controller did add a root key, but direct calls toserializer.as_json
andserializer.serializable_hash
with no arguments did not. Digging in, I think the problem is inActiveModel::Serializer:112-115
:that is,
@serialization_adapter_instance
is never set, and so it defaults to the hard-codedAttributes
adapter if it isn't passed in toserializable_hash
(which it isn't if you just useas_json
);