将活动模型序列化器从0.8迁移到0.10 rc2



我们已经有了一个使用Grape和Active Model serializer 0.8构建的API。现在我们想要使用0.10的所有缓存优点,因此迁移到新的向后不兼容的版本正在进行中。

目前有两个问题:

  1. 在序列化器内部用self.root=重新定义根键似乎是不可能的。例如,我们有SimpleUserSerializer,我们想要user而不是simple_user的根密钥。一种解决方案是在呈现序列化器时指定根,但随后我们需要在许多地方进行更改。是否有一种方法来重新定义这个序列化器的根键,而不管它在哪里/如何呈现?

  2. embed :ids, include: true选项不支持,应该通过适配器实现。是否有计划为遗留项目发布或维护与0.8兼容的适配器?

任何关于迁移的指导都会很有帮助,因为我找不到任何官方文档。

第一个问题可以通过定义返回根键的类方法root_name来解决。这可以在AMS测试中的fixture中看到。

还在写第二期

官方指南可能会有所帮助:https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/howto/upgrade_from_0_8_to_0_10.md

如果没有帮助,试一下:

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

:

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

完整的升级指南如下:http://engineering.liefery.com/2017/11/07/upgrading -活动-模型-序列化器——从0 - 8 - 0 - 10. - html

最新更新