-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add support for jsonapi top level member #1147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
require 'set' | ||
module ActiveModel | ||
class SerializableResource | ||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter]) | ||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, | ||
:jsonapi_toplevel_meta]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be a global config. I can't see any reason someone would want to configure any of this from the controller. Maybe if you're serving different versions of the JSON API standard, but that hasn't happened yet. There's a PR in the JSON API to add revision at various levels.. But I think that would still be handled via a global
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My thought was that people might (although ti wouldn't be great) want to handle two APIs with one app, maybe for migrating to a newer version of the standard or something. |
||
|
||
def initialize(resource, options = {}) | ||
@resource = resource | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ module Configuration | |
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer | ||
base.config.adapter = :flatten_json | ||
base.config.jsonapi_resource_type = :plural | ||
base.config.jsonapi_toplevel_member = false | ||
base.config.jsonapi_version = '1.0' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be in the JsonApi adapter IMHO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that makes sense. |
||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class 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 with_config(option, value) | ||
old_value = ActiveModel::Serializer.config[option] | ||
ActiveModel::Serializer.config[option] = value | ||
yield | ||
ensure | ||
ActiveModel::Serializer.config[option] = old_value | ||
end | ||
|
||
def test_disable_toplevel_jsonapi | ||
with_adapter :json_api do | ||
with_config(:jsonapi_toplevel_member, false) do | ||
hash = ActiveModel::SerializableResource.new(@post).serializable_hash | ||
assert_nil(hash[:jsonapi]) | ||
end | ||
end | ||
end | ||
|
||
def test_enable_toplevel_jsonapi | ||
with_adapter :json_api do | ||
with_config(:jsonapi_toplevel_member, true) do | ||
hash = ActiveModel::SerializableResource.new(@post).serializable_hash | ||
refute_nil(hash[:jsonapi]) | ||
end | ||
end | ||
end | ||
|
||
def test_default_toplevel_jsonapi_version | ||
with_adapter :json_api do | ||
with_config(:jsonapi_toplevel_member, true) do | ||
hash = ActiveModel::SerializableResource.new(@post).serializable_hash | ||
assert_equal('1.0', hash[:jsonapi][:version]) | ||
end | ||
end | ||
end | ||
|
||
def test_toplevel_jsonapi_no_meta | ||
with_adapter :json_api do | ||
with_config(:jsonapi_toplevel_member, true) do | ||
hash = ActiveModel::SerializableResource.new(@post).serializable_hash | ||
assert_nil(hash[:jsonapi][:meta]) | ||
end | ||
end | ||
end | ||
|
||
def test_toplevel_jsonapi_meta | ||
with_adapter :json_api do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for |
||
with_config(:jsonapi_toplevel_member, true) do | ||
hash = ActiveModel::SerializableResource.new(@post, jsonapi_toplevel_meta: 'custom').serializable_hash | ||
assert_equal('custom', hash[:jsonapi][:meta]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍