【问题标题】:Active Model Serializer not working with json_api adapterActive Model Serializer 不能与 json_api 适配器一起使用
【发布时间】:2015-11-09 06:08:18
【问题描述】:

我正在尝试对序列化程序中的关系使用自定义序列化程序并启用 json_api 适配器。但是,关系没有正确序列化(或者,更好的是,根本没有显示/序列化)。

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

以下主要数据是单个资源标识符对象, 引用相同的资源:

{ “数据”:{ “类型”:“文章”, "id": "1" } }

有没有办法防止获取资源标识符对象,而是获取实际数据?

【问题讨论】:

    标签: ruby-on-rails ruby json active-model-serializers


    【解决方案1】:

    根据json-api exmaples,关系仅返回idtype。如果您需要返回有关此关系的更多信息,您应该在您的渲染操作中添加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"
                }
            }
        ]
    }
    

    【讨论】:

    • @chris-peters 或 Bruno:有没有办法在序列化程序中默认包含关联,而不必在我们调用渲染的每个地方在控制器中执行它?是否有我们可以附加到序列化程序中的 has_one/has_many 关联的包含选项?
    • @geoboy 今天好像有人有同样的想法:github.com/rails-api/active_model_serializers/issues/1333 这个问题表明这种东西还没有。
    【解决方案2】:

    只是为了添加到@Bruno Bacarini 的答案,您还可以使用以下方法包含链式关联:

    render @posts, include: ['authors.profile', 'comments']
    

    来源:joaomdmoura's comment on github

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      相关资源
      最近更新 更多