Porting Active Serializer 0.8 to 0.10 rc2
We have an API built with Serializer Grape and Active Model 0.8. Now we would like to use all the goodness of caching from 0.10, so we are migrating to a new version that is incompatible with backward compatibility.
There are currently two problems:
-
Cannot override root key
self.root=
inside serializers. For example. we haveSimpleUserSerializer
, and we want theuser
root key to be added to instead ofsimple_user
. The solution is to specify the root when rendering the serializer, but then we need to make changes in many places. Is there a way to override the root key for these serializers no matter where / how it appears? -
embed :ids, include: true
the option is not supported and should probably be implemented via an adapter. Are there any plans to release or maintain a 0.8 compatible adapter for legacy projects?
Any guidance regarding migration would be helpful as I couldn't find the official documentation.
source to share
The first problem can be solved by defining a class method root_name
that returns the root key. This can be seen in luminaires in AMS tests .
Work continues on the second problem.
source to share
the official guide might come in handy: https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/howto/upgrade_from_0_8_to_0_10.md
If that doesn't help, try this:
In the previous version, we would specify the root as follows:
class UserSerializer < ActiveModel::Serializer
self.root = "application_user"
end
or:
class UserSerializer < ActiveModel::Serializer
root "application_user"
end
They both stopped working after the upgrade and we had to change it to:
class UserSerializer < ActiveModel::Serializer
type "application_user"
end
And this:
Root key not included in JSON
To fix that we had to configure json as the adapter (the new library default is attributes).
ActiveModelSerializers.config.adapter = :json
Complete upgrade guide here: http://engineering.liefery.com/2017/11/07/upgrading-active-model-serializers-from-0-8-to-0-10.html
source to share