Skip to content

Commit

Permalink
Fix built-in parsers and formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
namusyaka committed Mar 17, 2016
1 parent be09207 commit 7fdded0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 41 deletions.
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).

0.15.0 (3/8/2016)
=================
Expand Down
22 changes: 0 additions & 22 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,6 @@ module Exceptions
autoload :MethodNotAllowed
end

module ErrorFormatter
extend ActiveSupport::Autoload
autoload :Base
autoload :Json
autoload :Txt
autoload :Xml
end

module Formatter
extend ActiveSupport::Autoload
autoload :Json
autoload :SerializableHash
autoload :Txt
autoload :Xml
end

module Parser
extend ActiveSupport::Autoload
autoload :Json
autoload :Xml
end

module Middleware
extend ActiveSupport::Autoload
autoload :Base
Expand Down
21 changes: 14 additions & 7 deletions lib/grape/error_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
require 'grape/error_formatter/base'
require 'grape/error_formatter/json'
require 'grape/error_formatter/txt'
require 'grape/error_formatter/xml'

module Grape
module ErrorFormatter
class << self
FORMATTERS = {
serializable_hash: Grape::ErrorFormatter::Json,
json: Grape::ErrorFormatter::Json,
jsonapi: Grape::ErrorFormatter::Json,
txt: Grape::ErrorFormatter::Txt,
xml: Grape::ErrorFormatter::Xml
}

def builtin_formatters
{
serializable_hash: Grape::ErrorFormatter::Json,
json: Grape::ErrorFormatter::Json,
jsonapi: Grape::ErrorFormatter::Json,
txt: Grape::ErrorFormatter::Txt,
xml: Grape::ErrorFormatter::Xml
}
FORMATTERS
end

def formatters(options)
Expand Down
21 changes: 14 additions & 7 deletions lib/grape/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
require 'grape/formatter/json'
require 'grape/formatter/serializable_hash'
require 'grape/formatter/txt'
require 'grape/formatter/xml'

module Grape
module Formatter
class << self
FORMATTERS = {
json: Grape::Formatter::Json,
jsonapi: Grape::Formatter::Json,
serializable_hash: Grape::Formatter::SerializableHash,
txt: Grape::Formatter::Txt,
xml: Grape::Formatter::Xml
}

def builtin_formmaters
{
json: Grape::Formatter::Json,
jsonapi: Grape::Formatter::Json,
serializable_hash: Grape::Formatter::SerializableHash,
txt: Grape::Formatter::Txt,
xml: Grape::Formatter::Xml
}
FORMATTERS
end

def formatters(options)
Expand Down
15 changes: 10 additions & 5 deletions lib/grape/parser.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
require 'grape/parser/json'
require 'grape/parser/xml'

module Grape
module Parser
class << self
PARSERS = {
json: Grape::Parser::Json,
jsonapi: Grape::Parser::Json,
xml: Grape::Parser::Xml
}

def builtin_parsers
{
json: Grape::Parser::Json,
jsonapi: Grape::Parser::Json,
xml: Grape::Parser::Xml
}
PARSERS
end

def parsers(options)
Expand Down
16 changes: 16 additions & 0 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,20 @@ def to_xml
expect(subject.call(env)).to be_a(Grape::ServeFile::SendfileResponse)
end
end

context 'inheritable formatters' do
let(:app) { ->(_env) { [200, {}, ['']] } }
before do
class << Grape::Formatter
FORMATTERS[:invalid] = ->(_, _) { { message: 'invalid' }.to_json }
end
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

0 comments on commit 7fdded0

Please sign in to comment.