Skip to content

simple namespace implementation added #79

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

Merged
merged 7 commits into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This library is up-to-date with the finalized v1 JSON API spec.
* [Root links](#root-links)
* [Root errors](#root-errors)
* [Explicit serializer discovery](#explicit-serializer-discovery)
* [Namespace serializers](#namespace-serializers)
* [Relationships](#relationships)
* [Compound documents and includes](#compound-documents-and-includes)
* [Relationship path handling](#relationship-path-handling)
Expand Down Expand Up @@ -370,6 +371,36 @@ end

Now, when a `User` object is serialized, it will use the `SomeOtherNamespace::CustomUserSerializer`.

### Namespace serializers

Assume you have an API with multiple versions:

```ruby
module Api
module V1
class PostSerializer
include JSONAPI::Serializer
attribute :title
end
end
module V2
class PostSerializer
include JSONAPI::Serializer
attribute :name
end
end
end
```

With the namespace option you can choose which serializer is used.

```ruby
JSONAPI::Serializer.serialize(post, namespace: Api::V1)
JSONAPI::Serializer.serialize(post, namespace: Api::V2)
```

This option overrides the `jsonapi_serializer_class_name` method.

## Relationships

You can easily specify relationships with the `has_one` and `has_many` directives.
Expand Down
18 changes: 12 additions & 6 deletions lib/jsonapi-serializers/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,27 +216,31 @@ def evaluate_attr_or_block(attribute_name, attr_or_block)
protected :evaluate_attr_or_block
end

def self.find_serializer_class_name(object)
def self.find_serializer_class_name(object, options)
if options[:namespace]
return "#{options[:namespace]}::#{object.class.name}Serializer"
end
if object.respond_to?(:jsonapi_serializer_class_name)
return object.jsonapi_serializer_class_name.to_s
end
"#{object.class.name}Serializer"
end

def self.find_serializer_class(object)
class_name = find_serializer_class_name(object)
def self.find_serializer_class(object, options)
class_name = find_serializer_class_name(object, options)
class_name.constantize
end

def self.find_serializer(object, options)
find_serializer_class(object).new(object, options)
find_serializer_class(object, options).new(object, options)
end

def self.serialize(objects, options = {})
# Normalize option strings to symbols.
options[:is_collection] = options.delete('is_collection') || options[:is_collection] || false
options[:include] = options.delete('include') || options[:include]
options[:serializer] = options.delete('serializer') || options[:serializer]
options[:namespace] = options.delete('namespace') || options[:namespace]
options[:context] = options.delete('context') || options[:context] || {}
options[:skip_collection_check] = options.delete('skip_collection_check') || options[:skip_collection_check] || false
options[:base_url] = options.delete('base_url') || options[:base_url]
Expand All @@ -254,6 +258,7 @@ def self.serialize(objects, options = {})
passthrough_options = {
context: options[:context],
serializer: options[:serializer],
namespace: options[:namespace],
include: includes,
base_url: options[:base_url]
}
Expand Down Expand Up @@ -316,7 +321,8 @@ def self.serialize(objects, options = {})
included_passthrough_options = {}
included_passthrough_options[:base_url] = passthrough_options[:base_url]
included_passthrough_options[:context] = passthrough_options[:context]
included_passthrough_options[:serializer] = find_serializer_class(data[:object])
included_passthrough_options[:serializer] = find_serializer_class(data[:object], options)
included_passthrough_options[:namespace] = passthrough_options[:namespace]
included_passthrough_options[:include_linkages] = data[:include_linkages]
serialize_primary(data[:object], included_passthrough_options)
end
Expand Down Expand Up @@ -352,7 +358,7 @@ def self.single_error(attribute, message)
end

def self.serialize_primary(object, options = {})
serializer_class = options[:serializer] || find_serializer_class(object)
serializer_class = options[:serializer] || find_serializer_class(object, options)

# Spec: Primary data MUST be either:
# - a single resource object or null, for requests that target single resources.
Expand Down
44 changes: 44 additions & 0 deletions spec/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1065,4 +1065,48 @@ def read_attribute_for_validation(attr)
end.not_to raise_error
end
end

describe 'serializer with namespace option' do
it 'can serialize a simple object with namespace Api::V1' do
user = create(:user)
expect(JSONAPI::Serializer.serialize(user, {namespace: Api::V1})).to eq({
'data' => serialize_primary(user, {serializer: Api::V1::MyApp::UserSerializer}),
})
end

it 'handles recursive loading of relationships with namespaces' do
user = create(:user)
long_comments = create_list(:long_comment, 2, user: user)
post = create(:post, :with_author, long_comments: long_comments)
# Make sure each long-comment has a circular reference back to the post.
long_comments.each { |c| c.post = post }

expected_data = {
'data' => serialize_primary(post, {serializer: Api::V1::MyApp::PostSerializer}),
'included' => [
# Intermediates are included: long-comments, long-comments.post, and long-comments.post.author
# http://jsonapi.org/format/#document-structure-compound-documents
serialize_primary(post.long_comments.first, {
serializer: Api::V1::MyApp::LongCommentSerializer,
include_linkages: ['post']
}),
serialize_primary(post.long_comments.last, {
serializer: Api::V1::MyApp::LongCommentSerializer,
include_linkages: ['post']
}),
serialize_primary(post, {
serializer: Api::V1::MyApp::PostSerializer,
include_linkages: ['author', 'post.long-comments']
}),
serialize_primary(post.author, {serializer: Api::V1::MyApp::UserSerializer})
],
}
includes = ['long-comments.post.author']
actual_data = JSONAPI::Serializer.serialize(post, include: includes, namespace: Api::V1)
# Multiple expectations for better diff output for debugging.
expect(actual_data['data']).to eq(expected_data['data'])
expect(actual_data['included']).to eq(expected_data['included'])
expect(actual_data).to eq(expected_data)
end
end
end
31 changes: 31 additions & 0 deletions spec/support/serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,34 @@ class UserSerializer
attribute :name
end
end

module Api
module V1
module MyApp
class UserSerializer
include JSONAPI::Serializer

attribute :name
end

class PostSerializer
include JSONAPI::Serializer

attribute :title

has_one :author
has_many :long_comments
end

class LongCommentSerializer
include JSONAPI::Serializer

attribute :body
has_one :user

# Circular-reference back to post.
has_one :post
end
end
end
end