Skip to content

Commit 094b19a

Browse files
committed
Added appriasals for multi_json and multi_xml, removed from all tests.
1 parent 38911a4 commit 094b19a

16 files changed

+142
-31
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ matrix:
1717
gemfile: gemfiles/rails_edge.gemfile
1818
- rvm: 2.4.1
1919
gemfile: gemfiles/rails_5.gemfile
20+
- rvm: 2.4.1
21+
gemfile: gemfiles/multi_json.gemfile
22+
- rvm: 2.4.1
23+
gemfile: gemfiles/multi_xml.gemfile
2024
- rvm: 2.3.4
2125
gemfile: Gemfile
2226
- rvm: 2.3.4

Appraisals

+8
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ end
2222
appraise 'rack-edge' do
2323
gem 'rack', github: 'rack/rack'
2424
end
25+
26+
appraise 'multi_json' do
27+
gem 'multi_json'
28+
end
29+
30+
appraise 'multi_xml' do
31+
gem 'multi_xml'
32+
end

Gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ group :development do
2222
end
2323

2424
group :test do
25-
gem 'multi_json'
26-
gem 'multi_xml'
2725
gem 'grape-entity', '~> 0.6'
2826
gem 'maruku'
2927
gem 'rack-test'

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
- [CORS](#cors)
7070
- [Content-type](#content-type)
7171
- [API Data Formats](#api-data-formats)
72+
- [JSON and XML Processors](#json-and-xml-processors)
7273
- [RESTful Model Representations](#restful-model-representations)
7374
- [Grape Entities](#grape-entities)
7475
- [Hypermedia and Roar](#hypermedia-and-roar)
@@ -1146,7 +1147,7 @@ end
11461147
```
11471148

11481149
The `:values` option can also be supplied with a `Proc`, evaluated lazily with each request.
1149-
If the Proc has arity zero (i.e. it takes no arguments) it is expected to return either a list
1150+
If the Proc has arity zero (i.e. it takes no arguments) it is expected to return either a list
11501151
or a range which will then be used to validate the parameter.
11511152

11521153
For example, given a status model you may want to restrict by hashtags that you have
@@ -2564,6 +2565,10 @@ curl -X PUT -d 'data' 'http://localhost:9292/value' -H Content-Type:text/custom
25642565

25652566
You can disable parsing for a content-type with `nil`. For example, `parser :json, nil` will disable JSON parsing altogether. The request data is then available as-is in `env['api.request.body']`.
25662567

2568+
## JSON and XML Processors
2569+
2570+
Grape uses `JSON` and `ActiveSupport::XmlMini` for JSON and XML parsing by default. It also detects and supports [multi_json](https://github.com/intridea/multi_json) and [multi_xml](https://github.com/sferik/multi_xml). Adding those gems to your Gemfile will automatically enable them and allow you to swap the JSON and XML back-ends.
2571+
25672572
## RESTful Model Representations
25682573

25692574
Grape supports a range of ways to present your data with some help from a generic `present` method,

UPGRADING.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ Upgrading Grape
33

44
### Upgrading to >= 1.0.0
55

6+
#### Changes in XML and JSON Parsers
7+
8+
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`.
9+
10+
To restore previous behavior, add `multi_json` or `multi_xml` to your `Gemfile`, Grape will auto-detect it.
11+
12+
See [#1623](https://github.com/ruby-grape/grape/pull/1623) for more information.
13+
614
#### Changes in Parameter Class
715

816
The default class for `params` has changed from `Hashie::Mash` to `ActiveSupport::HashWithIndifferentAccess` and the `hashie` dependency has been removed. This means that by default you can no longer access parameters by method name.

gemfiles/multi_json.gemfile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This file was generated by Appraisal
2+
3+
source 'https://rubygems.org'
4+
5+
gem 'multi_json'
6+
7+
group :development, :test do
8+
gem 'bundler'
9+
gem 'rake'
10+
gem 'rubocop', '0.47.0'
11+
gem 'hashie'
12+
end
13+
14+
group :development do
15+
gem 'guard'
16+
gem 'guard-rspec'
17+
gem 'guard-rubocop'
18+
gem 'yard'
19+
gem 'appraisal'
20+
gem 'benchmark-ips'
21+
gem 'redcarpet'
22+
end
23+
24+
group :test do
25+
gem 'grape-entity', '~> 0.6'
26+
gem 'maruku'
27+
gem 'rack-test'
28+
gem 'rspec', '~> 3.0'
29+
gem 'cookiejar'
30+
gem 'rack-jsonp', require: 'rack/jsonp'
31+
gem 'mime-types'
32+
gem 'ruby-grape-danger', '~> 0.1.0', require: false
33+
gem 'coveralls', '~> 0.8.17', require: false
34+
end
35+
36+
gemspec path: '../'

gemfiles/multi_xml.gemfile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This file was generated by Appraisal
2+
3+
source 'https://rubygems.org'
4+
5+
gem 'multi_xml'
6+
7+
group :development, :test do
8+
gem 'bundler'
9+
gem 'rake'
10+
gem 'rubocop', '0.47.0'
11+
gem 'hashie'
12+
end
13+
14+
group :development do
15+
gem 'guard'
16+
gem 'guard-rspec'
17+
gem 'guard-rubocop'
18+
gem 'yard'
19+
gem 'appraisal'
20+
gem 'benchmark-ips'
21+
gem 'redcarpet'
22+
end
23+
24+
group :test do
25+
gem 'grape-entity', '~> 0.6'
26+
gem 'maruku'
27+
gem 'rack-test'
28+
gem 'rspec', '~> 3.0'
29+
gem 'cookiejar'
30+
gem 'rack-jsonp', require: 'rack/jsonp'
31+
gem 'mime-types'
32+
gem 'ruby-grape-danger', '~> 0.1.0', require: false
33+
gem 'coveralls', '~> 0.8.17', require: false
34+
end
35+
36+
gemspec path: '../'

gemfiles/rack_1.5.2.gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ group :development do
2222
end
2323

2424
group :test do
25-
gem 'multi_json'
26-
gem 'multi_xml'
2725
gem 'grape-entity', '~> 0.6'
2826
gem 'maruku'
2927
gem 'rack-test'

gemfiles/rack_edge.gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ group :development do
2222
end
2323

2424
group :test do
25-
gem 'multi_json'
26-
gem 'multi_xml'
2725
gem 'grape-entity', '~> 0.6'
2826
gem 'maruku'
2927
gem 'rack-test'

gemfiles/rails_3.gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ group :development do
2323
end
2424

2525
group :test do
26-
gem 'multi_json'
27-
gem 'multi_xml'
2826
gem 'grape-entity', '~> 0.6'
2927
gem 'maruku'
3028
gem 'rack-test'

gemfiles/rails_4.gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ group :development do
2222
end
2323

2424
group :test do
25-
gem 'multi_json'
26-
gem 'multi_xml'
2725
gem 'grape-entity', '~> 0.6'
2826
gem 'maruku'
2927
gem 'rack-test'

gemfiles/rails_5.gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ group :development do
2222
end
2323

2424
group :test do
25-
gem 'multi_json'
26-
gem 'multi_xml'
2725
gem 'grape-entity', '~> 0.6'
2826
gem 'maruku'
2927
gem 'rack-test'

gemfiles/rails_edge.gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ group :development do
2222
end
2323

2424
group :test do
25-
gem 'multi_json'
26-
gem 'multi_xml'
2725
gem 'grape-entity', '~> 0.6'
2826
gem 'maruku'
2927
gem 'rack-test'

spec/grape/api_spec.rb

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'spec_helper'
22
require 'shared/versioning_examples'
3-
# require 'grape-entity'
43

54
describe Grape::API do
65
subject { Class.new(Grape::API) }
@@ -2225,14 +2224,27 @@ def self.call(object, _env)
22252224
expect(last_response.body).to eql 'elpmis'
22262225
end
22272226
end
2228-
context 'multi_xml' do
2229-
it "doesn't parse yaml" do
2230-
subject.put :yaml do
2231-
params[:tag]
2227+
if Object.const_defined? :MultiXml
2228+
context 'multi_xml' do
2229+
it "doesn't parse yaml" do
2230+
subject.put :yaml do
2231+
params[:tag]
2232+
end
2233+
put '/yaml', '<tag type="symbol">a123</tag>', 'CONTENT_TYPE' => 'application/xml'
2234+
expect(last_response.status).to eq(400)
2235+
expect(last_response.body).to eql 'Disallowed type attribute: "symbol"'
2236+
end
2237+
end
2238+
else
2239+
context 'default xml parser' do
2240+
it 'parses symbols' do
2241+
subject.put :yaml do
2242+
params[:tag]
2243+
end
2244+
put '/yaml', '<tag type="symbol">a123</tag>', 'CONTENT_TYPE' => 'application/xml'
2245+
expect(last_response.status).to eq(200)
2246+
expect(last_response.body).to eql '{"type"=>"symbol", "__content__"=>"a123"}'
22322247
end
2233-
put '/yaml', '<tag type="symbol">a123</tag>', 'CONTENT_TYPE' => 'application/xml'
2234-
expect(last_response.status).to eq(400)
2235-
expect(last_response.body).to eql 'Disallowed type attribute: "symbol"'
22362248
end
22372249
end
22382250
context 'none parser class' do

spec/grape/endpoint_spec.rb

+19-7
Original file line numberDiff line numberDiff line change
@@ -882,14 +882,26 @@ def app
882882
expect(last_response.body).to eq('')
883883
end
884884

885-
it 'converts XML bodies to params' do
886-
post '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
887-
expect(last_response.body).to eq('Bobby T.')
888-
end
885+
if Object.const_defined? :MultiXml
886+
it 'converts XML bodies to params' do
887+
post '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
888+
expect(last_response.body).to eq('Bobby T.')
889+
end
889890

890-
it 'converts XML bodies to params' do
891-
put '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
892-
expect(last_response.body).to eq('Bobby T.')
891+
it 'converts XML bodies to params' do
892+
put '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
893+
expect(last_response.body).to eq('Bobby T.')
894+
end
895+
else
896+
it 'converts XML bodies to params' do
897+
post '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
898+
expect(last_response.body).to eq('{"__content__"=>"Bobby T."}')
899+
end
900+
901+
it 'converts XML bodies to params' do
902+
put '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
903+
expect(last_response.body).to eq('{"__content__"=>"Bobby T."}')
904+
end
893905
end
894906

895907
it 'does not include parameters not defined by the body' do

spec/grape/middleware/formatter_spec.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,11 @@ def to_xml
274274
'rack.input' => io,
275275
'CONTENT_LENGTH' => io.length
276276
)
277-
expect(subject.env['rack.request.form_hash']['thing']['name']).to eq('Test')
277+
if Object.const_defined? :MultiXml
278+
expect(subject.env['rack.request.form_hash']['thing']['name']).to eq('Test')
279+
else
280+
expect(subject.env['rack.request.form_hash']['thing']['name']['__content__']).to eq('Test')
281+
end
278282
end
279283
[Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES].flatten.each do |content_type|
280284
it "ignores #{content_type}" do

0 commit comments

Comments
 (0)