Skip to content
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

Add register keyword for adding customized parsers and formatters #1330

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

* [#1325](https://github.com/ruby-grape/grape/pull/1325): Params: Fix coerce_with helper with Array types - [@ngonzalez](https://github.com/ngonzalez).
* [#1326](https://github.com/ruby-grape/grape/pull/1326): Fix wrong behavior for OPTIONS and HEAD requests with catch-all - [@ekampp](https://github.com/ekampp), [@namusyaka](https://github.com/namusyaka).
* [#1330](https://github.com/ruby-grape/grape/pull/1330): Fix built-in parsers and formatters - [@namusyaka](https://github.com/namusyaka).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear what's being fixed. The built in parsers and formatters weren't broken really.


0.15.0 (3/8/2016)
=================
Expand Down
7 changes: 5 additions & 2 deletions lib/grape/error_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'grape/util/registrable'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autoload this?


module Grape
module ErrorFormatter
extend Registrable
class << self
def builtin_formatters
{
@builtin_formatters ||= {
serializable_hash: Grape::ErrorFormatter::Json,
json: Grape::ErrorFormatter::Json,
jsonapi: Grape::ErrorFormatter::Json,
Expand All @@ -12,7 +15,7 @@ def builtin_formatters
end

def formatters(options)
builtin_formatters.merge(options[:error_formatters] || {})
builtin_formatters.merge(default_elements).merge(options[:error_formatters] || {})
end

def formatter_for(api_format, options = {})
Expand Down
8 changes: 6 additions & 2 deletions lib/grape/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
require 'grape/util/registrable'

module Grape
module Formatter
extend Registrable

class << self
def builtin_formmaters
{
@builtin_formatters ||= {
json: Grape::Formatter::Json,
jsonapi: Grape::Formatter::Json,
serializable_hash: Grape::Formatter::SerializableHash,
Expand All @@ -12,7 +16,7 @@ def builtin_formmaters
end

def formatters(options)
builtin_formmaters.merge(options[:formatters] || {})
builtin_formmaters.merge(default_elements).merge(options[:formatters] || {})
end

def formatter_for(api_format, options = {})
Expand Down
8 changes: 6 additions & 2 deletions lib/grape/parser.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
require 'grape/util/registrable'

module Grape
module Parser
extend Registrable

class << self
def builtin_parsers
{
@builtin_parsers ||= {
json: Grape::Parser::Json,
jsonapi: Grape::Parser::Json,
xml: Grape::Parser::Xml
}
end

def parsers(options)
builtin_parsers.merge(options[:parsers] || {})
builtin_parsers.merge(default_elements).merge(options[:parsers] || {})
end

def parser_for(api_format, options = {})
Expand Down
11 changes: 11 additions & 0 deletions lib/grape/util/registrable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Grape
module Registrable
def default_elements
@default_elements ||= {}
end

def register(format, element)
default_elements[format] = element unless default_elements[format]
end
end
end
19 changes: 19 additions & 0 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,23 @@ def to_xml
expect(subject.call(env)).to be_a(Grape::ServeFile::SendfileResponse)
end
end

context 'inheritable formatters' do
class InvalidFormatter
def self.call(_, _)
{ message: 'invalid' }.to_json
end
end
let(:app) { ->(_env) { [200, {}, ['']] } }
before do
Grape::Formatter.register :invalid, InvalidFormatter
Grape::ContentTypes::CONTENT_TYPES[:invalid] = 'application/x-invalid'
end

it 'returns response by invalid formatter' do
env = { 'PATH_INFO' => '/hello.invalid', 'HTTP_ACCEPT' => 'application/x-invalid' }
_, _, bodies = *subject.call(env)
expect(bodies.body.first).to eq({ message: 'invalid' }.to_json)
end
end
end