active_model_serializers
active_model_serializers copied to clipboard
Introduces per-request adapter switching based on the request mime.
Purpose
The current behaviour is to always use the same adapter, this is not very desirable in more formal hypermedia conforming systems where content-negotiation determines the format which should be used. This PR introduces the option to let the system choose the adapter based on the negotiation results.
Changes
Content-type determination is left to ActionController::MimeResponds, so that users won't have to specify the adapter in every render call.
Caveats
- This implementation uses a non-existent virtual adapter
:mimeto let the subsystem know to look up the adapter dynamically, which may be considered inconsistent. - By using MimeResponds, an implicit dependency between mime types and serializers is created, which might not be suitable for projects needing to use different adapters under the same mime type.
- I'm not aware how these changes will affect view caching.
Related GitHub issues
@remear's comment on PR https://github.com/rails-api/active_model_serializers/pull/1082 was the cause to reimplement this feature. https://github.com/rails-api/active_model_serializers/issues/1039
@fletcher91, thanks for your PR! By analyzing the history of the files in this pull request, we identified @bf4, @domitian and @bacarini to be potential reviewers.