Skip to content

Commit

Permalink
Keep Grape::Json and Grape::Xml namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx committed Mar 24, 2024
1 parent 55e3753 commit c4e50ab
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 58 deletions.
2 changes: 1 addition & 1 deletion UPGRADING.md
Expand Up @@ -622,7 +622,7 @@ For PUT, POST, PATCH, and DELETE requests where a non-empty body and a "Content-

Grape no longer uses `multi_json` or `multi_xml` by default and uses `JSON` and `ActiveSupport::XmlMini` instead. This has no visible impact on JSON processing, but the default behavior of the XML parser has changed. For example, an XML POST containing `<user>Bobby T.</user>` was parsed as `Bobby T.` with `multi_xml`, and as now parsed as `{"__content__"=>"Bobby T."}` with `XmlMini`.

If you were using `MultiJson.load`, `MultiJson.dump` or `MultiXml.parse`, you can substitute those with `Grape::Util::Json.load`, `Grape::Util::Json.dump`, `::Grape::Util::Xml.parse`, or directly with `JSON.load`, `JSON.dump`, `XmlMini.parse`, etc.
If you were using `MultiJson.load`, `MultiJson.dump` or `MultiXml.parse`, you can substitute those with `Grape::Json.load`, `Grape::Json.dump`, `::Grape::Xml.parse`, or directly with `JSON.load`, `JSON.dump`, `XmlMini.parse`, etc.

To restore previous behavior, add `multi_json` or `multi_xml` to your `Gemfile` and `require` it.

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/error_formatter/json.rb
Expand Up @@ -12,7 +12,7 @@ def call(message, backtrace, options = {}, env = nil, original_exception = nil)
rescue_options = options[:rescue_options] || {}
result = result.merge(backtrace: backtrace) if rescue_options[:backtrace] && backtrace && !backtrace.empty?
result = result.merge(original_exception: original_exception.inspect) if rescue_options[:original_exception] && original_exception
::Grape::Util::Json.dump(result)
::Grape::Json.dump(result)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/error_formatter/txt.rb
Expand Up @@ -9,7 +9,7 @@ class << self
def call(message, backtrace, options = {}, env = nil, original_exception = nil)
message = present(message, env)

result = message.is_a?(Hash) ? ::Grape::Util::Json.dump(message) : message
result = message.is_a?(Hash) ? ::Grape::Json.dump(message) : message
rescue_options = options[:rescue_options] || {}
if rescue_options[:backtrace] && backtrace && !backtrace.empty?
result += "\r\n backtrace:"
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/formatter/json.rb
Expand Up @@ -7,7 +7,7 @@ class << self
def call(object, _env)
return object.to_json if object.respond_to?(:to_json)

::Grape::Util::Json.dump(object)
::Grape::Json.dump(object)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/formatter/serializable_hash.rb
Expand Up @@ -6,10 +6,10 @@ module SerializableHash
class << self
def call(object, _env)
return object if object.is_a?(String)
return ::Grape::Util::Json.dump(serialize(object)) if serializable?(object)
return ::Grape::Json.dump(serialize(object)) if serializable?(object)
return object.to_json if object.respond_to?(:to_json)

::Grape::Util::Json.dump(object)
::Grape::Json.dump(object)
end

private
Expand Down
10 changes: 10 additions & 0 deletions lib/grape/json.rb
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Grape
if defined?(::MultiJson)
Json = ::MultiJson
else
Json = ::JSON
Json::ParseError = Json::ParserError
end
end
4 changes: 2 additions & 2 deletions lib/grape/parser/json.rb
Expand Up @@ -5,8 +5,8 @@ module Parser
module Json
class << self
def call(object, _env)
::Grape::Util::Json.load(object)
rescue ::Grape::Util::Json::ParseError
::Grape::Json.load(object)
rescue ::Grape::Json::ParseError
# handle JSON parsing errors via the rescue handlers or provide error message
raise Grape::Exceptions::InvalidMessageBody.new('application/json')
end
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/parser/xml.rb
Expand Up @@ -5,8 +5,8 @@ module Parser
module Xml
class << self
def call(object, _env)
::Grape::Util::Xml.parse(object)
rescue ::Grape::Util::Xml::ParseError
::Grape::Xml.parse(object)
rescue ::Grape::Xml::ParseError
# handle XML parsing errors via the rescue handlers or provide error message
raise Grape::Exceptions::InvalidMessageBody.new('application/xml')
end
Expand Down
12 changes: 0 additions & 12 deletions lib/grape/util/json.rb

This file was deleted.

12 changes: 0 additions & 12 deletions lib/grape/util/xml.rb

This file was deleted.

10 changes: 10 additions & 0 deletions lib/grape/xml.rb
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Grape
if defined?(::MultiXml)
Xml = ::MultiXml
else
Xml = ::ActiveSupport::XmlMini
Xml::ParseError = StandardError
end
end
6 changes: 3 additions & 3 deletions spec/grape/api/invalid_format_spec.rb
Expand Up @@ -27,19 +27,19 @@ def app
it 'no format' do
get '/foo'
expect(last_response.status).to eq 200
expect(last_response.body).to eq(::Grape::Util::Json.dump(id: 'foo', format: nil))
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: nil))
end

it 'json format' do
get '/foo.json'
expect(last_response.status).to eq 200
expect(last_response.body).to eq(::Grape::Util::Json.dump(id: 'foo', format: 'json'))
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: 'json'))
end

it 'invalid format' do
get '/foo.invalid'
expect(last_response.status).to eq 200
expect(last_response.body).to eq(::Grape::Util::Json.dump(id: 'foo', format: 'invalid'))
expect(last_response.body).to eq(::Grape::Json.dump(id: 'foo', format: 'invalid'))
end
end
end
16 changes: 8 additions & 8 deletions spec/grape/api_spec.rb
Expand Up @@ -440,9 +440,9 @@ class DummyFormatClass
subject.send(verb) do
env['api.request.body']
end
send verb, '/', ::Grape::Util::Json.dump(object), 'CONTENT_TYPE' => 'application/json'
send verb, '/', ::Grape::Json.dump(object), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(verb == :post ? 201 : 200)
expect(last_response.body).to eql ::Grape::Util::Json.dump(object)
expect(last_response.body).to eql ::Grape::Json.dump(object)
expect(last_request.params).to eql({})
end

Expand All @@ -451,9 +451,9 @@ class DummyFormatClass
subject.send(verb) do
env['api.request.input']
end
send verb, '/', ::Grape::Util::Json.dump(object), 'CONTENT_TYPE' => 'application/json'
send verb, '/', ::Grape::Json.dump(object), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(verb == :post ? 201 : 200)
expect(last_response.body).to eql ::Grape::Util::Json.dump(object).to_json
expect(last_response.body).to eql ::Grape::Json.dump(object).to_json
end

context 'chunked transfer encoding' do
Expand All @@ -462,9 +462,9 @@ class DummyFormatClass
subject.send(verb) do
env['api.request.input']
end
send verb, '/', ::Grape::Util::Json.dump(object), 'CONTENT_TYPE' => 'application/json', 'HTTP_TRANSFER_ENCODING' => 'chunked', 'CONTENT_LENGTH' => nil
send verb, '/', ::Grape::Json.dump(object), 'CONTENT_TYPE' => 'application/json', 'HTTP_TRANSFER_ENCODING' => 'chunked', 'CONTENT_LENGTH' => nil
expect(last_response.status).to eq(verb == :post ? 201 : 200)
expect(last_response.body).to eql ::Grape::Util::Json.dump(object).to_json
expect(last_response.body).to eql ::Grape::Json.dump(object).to_json
end
end
end
Expand Down Expand Up @@ -2505,7 +2505,7 @@ def self.call(message, _backtrace, _option, _env, _original_exception)
raise 'rain!'
end
get '/exception'
json = ::Grape::Util::Json.load(last_response.body)
json = ::Grape::Json.load(last_response.body)
expect(json['error']).to eql 'rain!'
expect(json['backtrace'].length).to be > 0
end
Expand Down Expand Up @@ -3733,7 +3733,7 @@ def my_method

it 'path' do
get '/endpoint/options'
options = ::Grape::Util::Json.load(last_response.body)
options = ::Grape::Json.load(last_response.body)
expect(options['path']).to eq(['/endpoint/options'])
expect(options['source_location'][0]).to include 'api_spec.rb'
expect(options['source_location'][1].to_i).to be > 0
Expand Down
4 changes: 2 additions & 2 deletions spec/grape/endpoint/declared_spec.rb
Expand Up @@ -296,7 +296,7 @@ def app
''
end

post '/declared', ::Grape::Util::Json.dump(first: 'one', boolean: false), 'CONTENT_TYPE' => 'application/json'
post '/declared', ::Grape::Json.dump(first: 'one', boolean: false), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
end

Expand All @@ -311,7 +311,7 @@ def app
''
end

post '/declared', ::Grape::Util::Json.dump(first: 'one', second: nil), 'CONTENT_TYPE' => 'application/json'
post '/declared', ::Grape::Json.dump(first: 'one', second: nil), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
end

Expand Down
8 changes: 4 additions & 4 deletions spec/grape/endpoint_spec.rb
Expand Up @@ -375,7 +375,7 @@ def app
end

it 'converts JSON bodies to params' do
post '/request_body', ::Grape::Util::Json.dump(user: 'Bobby T.'), 'CONTENT_TYPE' => 'application/json'
post '/request_body', ::Grape::Json.dump(user: 'Bobby T.'), 'CONTENT_TYPE' => 'application/json'
expect(last_response.body).to eq('Bobby T.')
end

Expand Down Expand Up @@ -411,7 +411,7 @@ def app
error! 400, 'expected nil' if params[:version]
params[:user]
end
post '/omitted_params', ::Grape::Util::Json.dump(user: 'Bob'), 'CONTENT_TYPE' => 'application/json'
post '/omitted_params', ::Grape::Json.dump(user: 'Bob'), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('Bob')
end
Expand Down Expand Up @@ -468,7 +468,7 @@ def app
subject.put '/request_body' do
params[:user]
end
put '/request_body', ::Grape::Util::Json.dump(user: 'Bob'), 'CONTENT_TYPE' => 'text/plain'
put '/request_body', ::Grape::Json.dump(user: 'Bob'), 'CONTENT_TYPE' => 'text/plain'

expect(last_response.status).to eq(415)
expect(last_response.body).to eq('{"error":"The provided content-type \'text/plain\' is not supported."}')
Expand All @@ -482,7 +482,7 @@ def app
subject.post do
params[:data]
end
post '/', ::Grape::Util::Json.dump(data: { some: 'payload' }), 'CONTENT_TYPE' => 'application/json'
post '/', ::Grape::Json.dump(data: { some: 'payload' }), 'CONTENT_TYPE' => 'application/json'
end

it 'does not response with 406 for same type without params' do
Expand Down
2 changes: 1 addition & 1 deletion spec/grape/middleware/formatter_spec.rb
Expand Up @@ -13,7 +13,7 @@

it 'looks at the bodies for possibly serializable data' do
_, _, bodies = *subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json')
bodies.each { |b| expect(b).to eq(::Grape::Util::Json.dump(body)) } # rubocop:disable RSpec/IteratedExpectation
bodies.each { |b| expect(b).to eq(::Grape::Json.dump(body)) } # rubocop:disable RSpec/IteratedExpectation
end

context 'default format' do
Expand Down
8 changes: 4 additions & 4 deletions spec/grape/validations/validators/coerce_spec.rb
Expand Up @@ -655,19 +655,19 @@ def self.parse(_val)
params[:values]
end

post '/coerce_nested_strings', ::Grape::Util::Json.dump(values: 'a,b,c,d'), 'CONTENT_TYPE' => 'application/json'
post '/coerce_nested_strings', ::Grape::Json.dump(values: 'a,b,c,d'), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
expect(JSON.parse(last_response.body)).to eq([%w[a b c d]])

post '/coerce_nested_strings', ::Grape::Util::Json.dump(values: [%w[a c], %w[b]]), 'CONTENT_TYPE' => 'application/json'
post '/coerce_nested_strings', ::Grape::Json.dump(values: [%w[a c], %w[b]]), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
expect(JSON.parse(last_response.body)).to eq([%w[a c], %w[b]])

post '/coerce_nested_strings', ::Grape::Util::Json.dump(values: [[]]), 'CONTENT_TYPE' => 'application/json'
post '/coerce_nested_strings', ::Grape::Json.dump(values: [[]]), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(201)
expect(JSON.parse(last_response.body)).to eq([[]])

post '/coerce_nested_strings', ::Grape::Util::Json.dump(values: [['a', { bar: 0 }], ['b']]), 'CONTENT_TYPE' => 'application/json'
post '/coerce_nested_strings', ::Grape::Json.dump(values: [['a', { bar: 0 }], ['b']]), 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq(400)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/integration/multi_json/json_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe Grape::Util::Json, if: defined?(::MultiJson) do
describe Grape::Json, if: defined?(::MultiJson) do
it 'uses multi_json' do
expect(described_class).to eq(::MultiJson)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/multi_xml/xml_spec.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe Grape::Util::Xml, if: defined?(MultiXml) do
describe Grape::Xml, if: defined?(MultiXml) do
it 'uses multi_xml' do
expect(described_class).to eq(::MultiXml)
end
Expand Down

0 comments on commit c4e50ab

Please sign in to comment.