-
Notifications
You must be signed in to change notification settings - Fork 68
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
API Versioning: Supporting Active Model Serializers namespacing #49
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c2bdab8
Bump version to 1.4.0
syntaxTerr0r bec6d44
Rollback travis
syntaxTerr0r 4202e7e
Merge branch 'master' into versioned-api
syntaxTerr0r c57a388
Bump version to 1.4.0
syntaxTerr0r 8889b5a
Merge branch 'versioned-api' of https://github.com/syntaxTerr0r/grape…
syntaxTerr0r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ Use [active_model_serializers](https://github.com/rails-api/active_model_seriali | |
[![Build Status](https://api.travis-ci.org/jrhe/grape-active_model_serializers.png)](http://travis-ci.org/jrhe/grape-active_model_serializers) [![Dependency Status](https://gemnasium.com/jrhe/grape-active_model_serializers.png)](https://gemnasium.com/jrhe/grape-active_model_serializers) [![Code Climate](https://codeclimate.com/github/jrhe/grape-active_model_serializers.png)](https://codeclimate.com/github/jrhe/grape-active_model_serializers) | ||
|
||
## Breaking Changes | ||
#### v1.4.0 | ||
* *BREAKING* Changes behaviour in serializer namespacing when using Grape API versioning. See [API versioning](https://github.com/jrhe/grape-active_model_serializers#api-versioning) | ||
#### v1.0.0 | ||
* *BREAKING* Changes behaviour of root keys when serialising arrays. See [Array roots](https://github.com/jrhe/grape-active_model_serializers#array-roots) | ||
|
||
|
@@ -78,6 +80,83 @@ end | |
# root = people | ||
``` | ||
|
||
### API versioning | ||
|
||
If you haven't declared an API version in Grape, nothing change. | ||
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. nothing changes or no changes are required. |
||
|
||
If your Grape API is versioned, which means you have declared at least one version in Grape, ie: | ||
|
||
```ruby | ||
module CandyBar | ||
class Core < Grape::API | ||
version 'candy_bar', using: :header, vendor: 'acme' | ||
|
||
# My shiny endpoints | ||
# ... | ||
end | ||
end | ||
|
||
module Chocolate | ||
class Core < Grape::API | ||
version 'chocolate', using: :header, vendor: 'acme' | ||
|
||
# My shiny endpoints | ||
# ... | ||
end | ||
end | ||
|
||
class API < Grape::API | ||
format :json | ||
formatter :json, Grape::Formatter::ActiveModelSerializers | ||
|
||
mount CandyBar::Core | ||
mount Chocolate::Core | ||
end | ||
``` | ||
|
||
You'll have to namespace your serializers according to each version, ie: | ||
|
||
```ruby | ||
module CandyBar | ||
class UserSerializer < ActiveModel::Serializer | ||
attributes :first_name, :last_name, :email | ||
end | ||
end | ||
|
||
module Chocolate | ||
class UserSerializer < ActiveModel::Serializer | ||
attributes :first_name, :last_name | ||
end | ||
end | ||
``` | ||
|
||
Which allow you to keep your serializers easier to maintain and more organized. | ||
|
||
``` | ||
app | ||
└── api | ||
├── chocolate | ||
└── core.rb | ||
└── candy_bar | ||
└── core.rb | ||
api.rb | ||
└── serializers | ||
├── chocolate | ||
└── user_serializer.rb | ||
└── candy_bar | ||
└── user_serializer.rb | ||
``` | ||
|
||
or alternatively: | ||
|
||
``` | ||
└── serializers | ||
├── chocolate_user_serializer.rb | ||
└── candy_bar_user_serializer.rb | ||
``` | ||
|
||
Thus, ActiveModelSerializer will fetch automatically the right serializer to render. | ||
|
||
### Manually specifying serializer options | ||
|
||
```ruby | ||
|
@@ -185,6 +264,8 @@ Enjoy :) | |
#### Next | ||
* Adds support for Grape 0.10.x | ||
|
||
#### v1.4.0 | ||
* Adds support for active model serializer namespace | ||
|
||
#### v1.2.1 | ||
* Adds support for active model serializer 0.9.x | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,12 @@ def call(resource, env) | |
def fetch_serializer(resource, env) | ||
endpoint = env['api.endpoint'] | ||
options = build_options_from_endpoint(endpoint) | ||
ams_options = {}.tap do |ns| | ||
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. This is almost too much Ruby IMO :) ams_options = {}
ams_options[:namespace] = options[:version].try(:classify) if options.try(:[], :version) |
||
# Extracting declared version from Grape | ||
ns[:namespace] = options[:version].try(:classify) if options.try(:[], :version) | ||
end | ||
|
||
serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource)) | ||
serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource, ams_options)) | ||
return nil unless serializer | ||
|
||
options[:scope] = endpoint unless options.key?(:scope) | ||
|
@@ -28,7 +32,7 @@ def fetch_serializer(resource, env) | |
def other_options(env) | ||
options = {} | ||
ams_meta = env['ams_meta'] || {} | ||
meta = ams_meta.delete(:meta) | ||
meta = ams_meta.delete(:meta) | ||
meta_key = ams_meta.delete(:meta_key) | ||
options[:meta_key] = meta_key if meta && meta_key | ||
options[meta_key || :meta] = meta if meta | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Grape | ||
module ActiveModelSerializers | ||
VERSION = '1.3.1' | ||
VERSION = '1.4.0' | ||
end | ||
end |
67 changes: 67 additions & 0 deletions
67
spec/grape-active_model_serializers/versioned_api_formatter_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require 'spec_helper' | ||
require 'grape-active_model_serializers/formatter' | ||
|
||
describe Grape::Formatter::ActiveModelSerializers do | ||
describe 'with a versioned API' do | ||
subject { Grape::Formatter::ActiveModelSerializers } | ||
|
||
describe 'serializer options from namespace' do | ||
let(:app) { Class.new(Grape::API) } | ||
|
||
before do | ||
app.format :json | ||
app.formatter :json, Grape::Formatter::ActiveModelSerializers | ||
app.version 'v1', using: :param | ||
|
||
app.namespace('space') do |ns| | ||
ns.get('/', root: false, apiver: 'v1') do | ||
{ user: { first_name: 'JR', last_name: 'HE', email: '[email protected]' } } | ||
end | ||
end | ||
end | ||
|
||
it 'should read serializer options like "root"' do | ||
expect(described_class.build_options_from_endpoint(app.endpoints.first)).to include :root | ||
end | ||
end | ||
|
||
describe '.fetch_serializer' do | ||
let(:user) { User.new(first_name: 'John', email: '[email protected]') } | ||
|
||
if Grape::Util.const_defined?('InheritableSetting') | ||
let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo', version: 'v1', root: false) } | ||
else | ||
let(:endpoint) { Grape::Endpoint.new({}, path: '/', method: 'foo', version: 'v1', root: false) } | ||
end | ||
|
||
let(:env) { { 'api.endpoint' => endpoint } } | ||
|
||
before do | ||
def endpoint.current_user | ||
@current_user ||= User.new(first_name: 'Current user') | ||
end | ||
|
||
def endpoint.default_serializer_options | ||
{ only: :only, except: :except } | ||
end | ||
end | ||
|
||
subject { described_class.fetch_serializer(user, env) } | ||
|
||
it { should be_a V1::UserSerializer } | ||
|
||
it 'should have correct scope set' do | ||
expect(subject.scope.current_user).to eq(endpoint.current_user) | ||
end | ||
|
||
it 'should read default serializer options' do | ||
expect(subject.instance_variable_get('@only')).to eq([:only]) | ||
expect(subject.instance_variable_get('@except')).to eq([:except]) | ||
end | ||
|
||
it 'should read serializer options like "root"' do | ||
expect(described_class.build_options_from_endpoint(endpoint).keys).to include :root | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module V1 | ||
class UserSerializer < ActiveModel::Serializer | ||
attributes :first_name, :last_name, :email | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Lets move this into an UPGRADING document like https://github.com/ruby-grape/grape/blob/master/UPGRADING.md?