活动模型序列化程序不适用于 json_api 适配器
Active Model Serializer not working with json_api adapter
我正在尝试为序列化程序中的关系使用自定义序列化程序并启用 json_api 适配器。然而,这些关系没有正确序列化(或者,更好的是,根本没有 displayed/serialized)。
PostController.rb
def index
render json: Post.all, each_serializer: Serializers::PostSerializer
end
序列化器
module Api
module V1
module Serializers
class PostSerializer < ActiveModel::Serializer
attributes :title, :id
belongs_to :author, serializer: UserSerializer
has_many :post_sections, serializer: PostSectionSerializer
end
end
end
end
JSON 输出:
{
"data": [
{
"attributes": {
"title": "Test Title"
},
"id": "1",
"relationships": {
"author": {
"data": {
"id": "1",
"type": "users"
}
},
"post_sections": {
"data": [
{
"id": "1",
"type": "post_sections"
}
]
}
},
"type": "posts"
}
]
}
如您所见,关系未实现,只有当我为关系指定自定义序列化器时才会发生这种情况!!
如果我这样做:
module Api
module V1
module Serializers
class PostSerializer < ActiveModel::Serializer
attributes :title, :id
belongs_to :author # no custom serializer!
has_many :post_sections # no custom serializer!
end
end
end
end
关系显示正确,但未使用自定义序列化程序...
这里有什么问题?
编辑
根据json API 1.0 Format,我得到的就是所谓的resource identifier object
。
The following primary data is a single resource identifier object that
references the same resource:
{ "data": {
"type": "articles",
"id": "1" } }
有没有办法阻止获取资源标识符对象,而是获取实际数据?
只有 return 的关系 id
和 type
根据 json-api exmaples。如果您需要 return 有关此关系的更多信息,您应该在渲染操作中添加 include
选项。
例如
PostController.rb
class PostsController < ApplicationController
def show
render json: @post, include: 'comments'
end
end
序列化程序
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :content
has_many :comment, serializer: CommentSerializer
end
class CommentSerializer < ActiveModel::Serializer
attributes :id, :title, :content
end
JSON 输出:
{
"data": {
"id": "1",
"type": "post",
"attributes": {
"title": "bla",
"content": "bla"
},
"relationships": {
"comment": {
"data": [
{
"type": "comments",
"id": "1"
}
]
}
}
},
"included": {
{
"id": "1",
"type": "comments",
"attributes": {
"title": "test",
"content": "test"
}
}
]
}
只是为了添加到@Bruno Bacarini 的回答中,您还可以通过以下方式包含链式关联:
render @posts, include: ['authors.profile', 'comments']
我正在尝试为序列化程序中的关系使用自定义序列化程序并启用 json_api 适配器。然而,这些关系没有正确序列化(或者,更好的是,根本没有 displayed/serialized)。
PostController.rb
def index
render json: Post.all, each_serializer: Serializers::PostSerializer
end
序列化器
module Api
module V1
module Serializers
class PostSerializer < ActiveModel::Serializer
attributes :title, :id
belongs_to :author, serializer: UserSerializer
has_many :post_sections, serializer: PostSectionSerializer
end
end
end
end
JSON 输出:
{
"data": [
{
"attributes": {
"title": "Test Title"
},
"id": "1",
"relationships": {
"author": {
"data": {
"id": "1",
"type": "users"
}
},
"post_sections": {
"data": [
{
"id": "1",
"type": "post_sections"
}
]
}
},
"type": "posts"
}
]
}
如您所见,关系未实现,只有当我为关系指定自定义序列化器时才会发生这种情况!!
如果我这样做:
module Api
module V1
module Serializers
class PostSerializer < ActiveModel::Serializer
attributes :title, :id
belongs_to :author # no custom serializer!
has_many :post_sections # no custom serializer!
end
end
end
end
关系显示正确,但未使用自定义序列化程序... 这里有什么问题?
编辑
根据json API 1.0 Format,我得到的就是所谓的resource identifier object
。
The following primary data is a single resource identifier object that references the same resource:
{ "data": { "type": "articles", "id": "1" } }
有没有办法阻止获取资源标识符对象,而是获取实际数据?
只有 return 的关系 id
和 type
根据 json-api exmaples。如果您需要 return 有关此关系的更多信息,您应该在渲染操作中添加 include
选项。
例如
PostController.rb
class PostsController < ApplicationController
def show
render json: @post, include: 'comments'
end
end
序列化程序
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :content
has_many :comment, serializer: CommentSerializer
end
class CommentSerializer < ActiveModel::Serializer
attributes :id, :title, :content
end
JSON 输出:
{
"data": {
"id": "1",
"type": "post",
"attributes": {
"title": "bla",
"content": "bla"
},
"relationships": {
"comment": {
"data": [
{
"type": "comments",
"id": "1"
}
]
}
}
},
"included": {
{
"id": "1",
"type": "comments",
"attributes": {
"title": "test",
"content": "test"
}
}
]
}
只是为了添加到@Bruno Bacarini 的回答中,您还可以通过以下方式包含链式关联:
render @posts, include: ['authors.profile', 'comments']