Skip to content

Commit

Permalink
Added appriasals for multi_json and multi_xml, removed from all tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jun 13, 2017
1 parent 38911a4 commit ddf5f89
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ matrix:
gemfile: gemfiles/rails_edge.gemfile
- rvm: 2.4.1
gemfile: gemfiles/rails_5.gemfile
- rvm: 2.4.1
gemfile: gemfiles/multi_json.gemfile
- rvm: 2.4.1
gemfile: gemfiles/multi_xml.gemfile
- rvm: 2.3.4
gemfile: Gemfile
- rvm: 2.3.4
Expand Down
8 changes: 8 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ end
appraise 'rack-edge' do
gem 'rack', github: 'rack/rack'
end

appraise 'multi_json' do
gem 'multi_json'
end

appraise 'multi_xml' do
gem 'multi_xml'
end
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ group :development do
end

group :test do
gem 'multi_json'
gem 'multi_xml'
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'rack-test'
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
- [CORS](#cors)
- [Content-type](#content-type)
- [API Data Formats](#api-data-formats)
- [JSON and XML Processors](#json-and-xml-processors)
- [RESTful Model Representations](#restful-model-representations)
- [Grape Entities](#grape-entities)
- [Hypermedia and Roar](#hypermedia-and-roar)
Expand Down Expand Up @@ -1146,7 +1147,7 @@ end
```

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

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

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']`.

## JSON and XML Processors

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.

## RESTful Model Representations

Grape supports a range of ways to present your data with some help from a generic `present` method,
Expand Down
8 changes: 8 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ Upgrading Grape

### Upgrading to >= 1.0.0

#### Changes in XML and JSON Parsers

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`.

To restore previous behavior, add `multi_json` or `multi_xml` to your `Gemfile`, Grape will auto-detect it.

See [#1623](https://github.com/ruby-grape/grape/pull/1623) for more information.

#### Changes in Parameter Class

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.
Expand Down
36 changes: 36 additions & 0 deletions gemfiles/multi_json.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file was generated by Appraisal

source 'https://rubygems.org'

gem 'multi_json'

group :development, :test do
gem 'bundler'
gem 'rake'
gem 'rubocop', '0.47.0'
gem 'hashie'
end

group :development do
gem 'guard'
gem 'guard-rspec'
gem 'guard-rubocop'
gem 'yard'
gem 'appraisal'
gem 'benchmark-ips'
gem 'redcarpet'
end

group :test do
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'rack-test'
gem 'rspec', '~> 3.0'
gem 'cookiejar'
gem 'rack-jsonp', require: 'rack/jsonp'
gem 'mime-types'
gem 'ruby-grape-danger', '~> 0.1.0', require: false
gem 'coveralls', '~> 0.8.17', require: false
end

gemspec path: '../'
36 changes: 36 additions & 0 deletions gemfiles/multi_xml.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file was generated by Appraisal

source 'https://rubygems.org'

gem 'multi_xml'

group :development, :test do
gem 'bundler'
gem 'rake'
gem 'rubocop', '0.47.0'
gem 'hashie'
end

group :development do
gem 'guard'
gem 'guard-rspec'
gem 'guard-rubocop'
gem 'yard'
gem 'appraisal'
gem 'benchmark-ips'
gem 'redcarpet'
end

group :test do
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'rack-test'
gem 'rspec', '~> 3.0'
gem 'cookiejar'
gem 'rack-jsonp', require: 'rack/jsonp'
gem 'mime-types'
gem 'ruby-grape-danger', '~> 0.1.0', require: false
gem 'coveralls', '~> 0.8.17', require: false
end

gemspec path: '../'
2 changes: 0 additions & 2 deletions gemfiles/rack_1.5.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ group :development do
end

group :test do
gem 'multi_json'
gem 'multi_xml'
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'rack-test'
Expand Down
2 changes: 0 additions & 2 deletions gemfiles/rack_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ group :development do
end

group :test do
gem 'multi_json'
gem 'multi_xml'
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'rack-test'
Expand Down
2 changes: 0 additions & 2 deletions gemfiles/rails_3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ group :development do
end

group :test do
gem 'multi_json'
gem 'multi_xml'
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'rack-test'
Expand Down
2 changes: 0 additions & 2 deletions gemfiles/rails_4.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ group :development do
end

group :test do
gem 'multi_json'
gem 'multi_xml'
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'rack-test'
Expand Down
2 changes: 0 additions & 2 deletions gemfiles/rails_5.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ group :development do
end

group :test do
gem 'multi_json'
gem 'multi_xml'
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'rack-test'
Expand Down
2 changes: 0 additions & 2 deletions gemfiles/rails_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ group :development do
end

group :test do
gem 'multi_json'
gem 'multi_xml'
gem 'grape-entity', '~> 0.6'
gem 'maruku'
gem 'rack-test'
Expand Down
8 changes: 4 additions & 4 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2225,14 +2225,14 @@ def self.call(object, _env)
expect(last_response.body).to eql 'elpmis'
end
end
context 'multi_xml' do
it "doesn't parse yaml" do
context 'default xml parser' do
it 'parses symbols' do
subject.put :yaml do
params[:tag]
end
put '/yaml', '<tag type="symbol">a123</tag>', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.status).to eq(400)
expect(last_response.body).to eql 'Disallowed type attribute: "symbol"'
expect(last_response.status).to eq(200)
expect(last_response.body).to eql '{"type"=>"symbol", "__content__"=>"a123"}'
end
end
context 'none parser class' do
Expand Down
4 changes: 2 additions & 2 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -884,12 +884,12 @@ def app

it 'converts XML bodies to params' do
post '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.body).to eq('Bobby T.')
expect(last_response.body).to eq('{"__content__"=>"Bobby T."}')
end

it 'converts XML bodies to params' do
put '/request_body', '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml'
expect(last_response.body).to eq('Bobby T.')
expect(last_response.body).to eq('{"__content__"=>"Bobby T."}')
end

it 'does not include parameters not defined by the body' do
Expand Down
2 changes: 1 addition & 1 deletion spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def to_xml
'rack.input' => io,
'CONTENT_LENGTH' => io.length
)
expect(subject.env['rack.request.form_hash']['thing']['name']).to eq('Test')
expect(subject.env['rack.request.form_hash']['thing']['name']['__content__']).to eq('Test')
end
[Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES].flatten.each do |content_type|
it "ignores #{content_type}" do
Expand Down

0 comments on commit ddf5f89

Please sign in to comment.