Merge pull request #1050 from bf4/json_api_member

Add top-level jsonapi member to JSON API adapter
This commit is contained in:
Benjamin Fleischer 2015-10-01 23:03:11 -05:00
commit a2bfe190e3
7 changed files with 157 additions and 6 deletions

View File

@ -17,6 +17,7 @@ Features:
- [#1158](https://github.com/rails-api/active_model_serializers/pull/1158) Add support for wildcards in `include` option (@beauby) - [#1158](https://github.com/rails-api/active_model_serializers/pull/1158) Add support for wildcards in `include` option (@beauby)
- [#1127](https://github.com/rails-api/active_model_serializers/pull/1127) Add support for nested - [#1127](https://github.com/rails-api/active_model_serializers/pull/1127) Add support for nested
associations for JSON and Attributes adapters via the `include` option (@NullVoxPopuli, @beauby). associations for JSON and Attributes adapters via the `include` option (@NullVoxPopuli, @beauby).
- [#1050](https://github.com/rails-api/active_model_serializers/pull/1050) Add support for toplevel jsonapi member (@beauby, @bf4)
Fixes: Fixes:

View File

@ -9,3 +9,11 @@ The following configuration options can be set on `ActiveModel::Serializer.confi
## JSON API ## JSON API
- `jsonapi_resource_type`: Whether the `type` attributes of resources should be singular or plural. Possible values: `:singular, :plural`. Default: `:plural`. - `jsonapi_resource_type`: Whether the `type` attributes of resources should be singular or plural. Possible values: `:singular, :plural`. Default: `:plural`.
- `jsonapi_include_toplevel_object`: Whether to include a [top level JSON API member](http://jsonapi.org/format/#document-jsonapi-object)
in the response document.
Default: `false`.
- Used when `jsonapi_include_toplevel_object` is `true`:
- `jsonapi_version`: The latest version of the spec the API conforms to.
Default: `'1.0'`.
- `jsonapi_toplevel_meta`: Optional metadata. Not included if empty.
Default: `{}`.

View File

@ -1,5 +1,4 @@
require 'thread_safe' require 'thread_safe'
require 'active_model/serializer/adapter'
require 'active_model/serializer/array_serializer' require 'active_model/serializer/array_serializer'
require 'active_model/serializer/include_tree' require 'active_model/serializer/include_tree'
require 'active_model/serializer/associations' require 'active_model/serializer/associations'
@ -11,6 +10,7 @@ module ActiveModel
class Serializer class Serializer
include Configuration include Configuration
include Associations include Associations
require 'active_model/serializer/adapter'
# Matches # Matches
# "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'" # "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"

View File

@ -6,6 +6,41 @@ module ActiveModel
autoload :PaginationLinks autoload :PaginationLinks
autoload :FragmentCache autoload :FragmentCache
# TODO: if we like this abstraction and other API objects to it,
# then extract to its own file and require it.
module ApiObjects
module JsonApi
ActiveModel::Serializer.config.jsonapi_version = '1.0'
ActiveModel::Serializer.config.jsonapi_toplevel_meta = {}
# Make JSON API top-level jsonapi member opt-in
# ref: http://jsonapi.org/format/#document-top-level
ActiveModel::Serializer.config.jsonapi_include_toplevel_object = false
module_function
def add!(hash)
hash.merge!(object) if include_object?
end
def include_object?
ActiveModel::Serializer.config.jsonapi_include_toplevel_object
end
# TODO: see if we can cache this
def object
object = {
jsonapi: {
version: ActiveModel::Serializer.config.jsonapi_version,
meta: ActiveModel::Serializer.config.jsonapi_toplevel_meta
}
}
object[:jsonapi].reject! { |_, v| v.blank? }
object
end
end
end
def initialize(serializer, options = {}) def initialize(serializer, options = {})
super super
@include_tree = IncludeTree.from_include_args(options[:include]) @include_tree = IncludeTree.from_include_args(options[:include])
@ -21,11 +56,16 @@ module ActiveModel
def serializable_hash(options = nil) def serializable_hash(options = nil)
options ||= {} options ||= {}
if serializer.respond_to?(:each) hash =
serializable_hash_for_collection(options) if serializer.respond_to?(:each)
else serializable_hash_for_collection(options)
serializable_hash_for_single_resource(options) else
end serializable_hash_for_single_resource(options)
end
ApiObjects::JsonApi.add!(hash)
hash
end end
def fragment_cache(cached_hash, non_cached_hash) def fragment_cache(cached_hash, non_cached_hash)

View File

@ -4,6 +4,8 @@ module ActiveModel
include ActiveSupport::Configurable include ActiveSupport::Configurable
extend ActiveSupport::Concern extend ActiveSupport::Concern
# Configuration options may also be set in
# Serializers and Adapters
included do |base| included do |base|
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
base.config.adapter = :attributes base.config.adapter = :attributes

View File

@ -0,0 +1,84 @@
require 'test_helper'
module ActiveModel
class Serializer
module Adapter
class JsonApi
class TopLevelJsonApiTest < Minitest::Test
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@author.bio = nil
@author.roles = []
@blog = Blog.new(id: 23, name: 'AMS Blog')
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@post.comments = [@comment]
@post.blog = @blog
@anonymous_post.comments = []
@anonymous_post.blog = nil
@comment.post = @post
@comment.author = nil
@post.author = @author
@anonymous_post.author = nil
@blog = Blog.new(id: 1, name: 'My Blog!!')
@blog.writer = @author
@blog.articles = [@post, @anonymous_post]
@author.posts = []
end
def test_toplevel_jsonapi_defaults_to_false
assert_equal config.fetch(:jsonapi_include_toplevel_object), false
end
def test_disable_toplevel_jsonapi
with_config(jsonapi_include_toplevel_object: false) do
hash = serialize(@post)
assert_nil(hash[:jsonapi])
end
end
def test_enable_toplevel_jsonapi
with_config(jsonapi_include_toplevel_object: true) do
hash = serialize(@post)
refute_nil(hash[:jsonapi])
end
end
def test_default_toplevel_jsonapi_version
with_config(jsonapi_include_toplevel_object: true) do
hash = serialize(@post)
assert_equal('1.0', hash[:jsonapi][:version])
end
end
def test_toplevel_jsonapi_no_meta
with_config(jsonapi_include_toplevel_object: true) do
hash = serialize(@post)
assert_nil(hash[:jsonapi][:meta])
end
end
def test_toplevel_jsonapi_meta
new_config = {
jsonapi_include_toplevel_object: true,
jsonapi_toplevel_meta: {
'copyright' => 'Copyright 2015 Example Corp.'
}
}
with_config(new_config) do
hash = serialize(@post)
assert_equal(new_config[:jsonapi_toplevel_meta], hash.fetch(:jsonapi).fetch(:meta))
end
end
private
def serialize(resource, options = {})
serializable(resource, { adapter: :json_api }.merge!(options)).serializable_hash
end
end
end
end
end
end

View File

@ -1,4 +1,8 @@
module SerializationTesting module SerializationTesting
def config
ActiveModel::Serializer.config
end
private private
def generate_cached_serializer(obj) def generate_cached_serializer(obj)
@ -18,6 +22,18 @@ module SerializationTesting
ActiveModel::Serializer.config.adapter = old_adapter ActiveModel::Serializer.config.adapter = old_adapter
end end
alias_method :with_configured_adapter, :with_adapter alias_method :with_configured_adapter, :with_adapter
def with_config(hash)
old_config = config.dup
ActiveModel::Serializer.config.update(hash)
yield
ensure
ActiveModel::Serializer.config.replace(old_config)
end
def serializable(resource, options = {})
ActiveModel::SerializableResource.new(resource, options)
end
end end
class Minitest::Test class Minitest::Test