我们已经用葡萄和活动模型序列化器0.8构建了一个API。现在,我们希望使用0.10中的所有缓存优点,因此向新的向后兼容版本的迁移正在进行中。
目前有两个问题:
self.root=重新定义根键似乎是不可能的。例如,我们有SimpleUserSerializer,我们想要user的根键,而不是simple_user。一种解决方案是在呈现序列化程序时指定根,但随后我们需要在许多地方进行更改。是否有一种方法可以重新定义这个序列化程序的根键,而不管它是在哪里/如何呈现的?embed :ids, include: true选项不受支持,可能应该通过适配器实现。是否有计划为遗留项目发布或维护与0.8兼容的适配器?任何关于迁移的指导都是有帮助的,因为我找不到任何正式文档。
发布于 2015-07-24 07:57:33
第一个问题可以通过定义类方法root_name来解决,它返回根键。这可以在AMS测试中的夹具中看到。
还在研究第二个问题。
发布于 2019-06-07 17:16:34
官方指南可能会有所帮助: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-active-model-serializers-from-0-8-to-0-10.html
https://stackoverflow.com/questions/31580178
复制相似问题