Skip to content
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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ jobs:
- ruby: '4.0'
gemfile: gemfiles/multi_xml.gemfile
specs: 'spec/integration/multi_xml'
- ruby: '4.0'
gemfile: gemfiles/multi_xml_0_8.gemfile
specs: 'spec/integration/multi_xml'
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/${{ matrix.gemfile }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
* [#2706](https://github.com/ruby-grape/grape/pull/2706): Fix `optional :foo, message: 'oops'` raising `UnknownValidator` - [@ericproulx](https://github.com/ericproulx).
* [#2751](https://github.com/ruby-grape/grape/pull/2751): Fix structured error messages leaking the raw i18n key for an undefined optional step such as `summary` (closes #2748) - [@ericproulx](https://github.com/ericproulx).
* [#2759](https://github.com/ruby-grape/grape/pull/2759): Use `create_additions: false` in `Grape::Json.load` to prevent object instantiation via the `json_class` key when using the stdlib JSON fallback - [@dblock](https://github.com/dblock).
* [#2765](https://github.com/ruby-grape/grape/pull/2765): Detect the `MultiXML` constant to avoid the multi_xml 0.9 `MultiXml` deprecation - [@ericproulx](https://github.com/ericproulx).
* [#2764](https://github.com/ruby-grape/grape/pull/2764): Route `Grape::Json` through the non-deprecated `MultiJSON` API - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/multi_xml.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

gem 'multi_xml'
gem 'multi_xml', '>= 0.9'

eval_gemfile '../Gemfile'
5 changes: 5 additions & 0 deletions gemfiles/multi_xml_0_8.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

gem 'multi_xml', '< 0.9'

eval_gemfile '../Gemfile'
9 changes: 8 additions & 1 deletion lib/grape/xml.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# frozen_string_literal: true

module Grape
if defined?(::MultiXml)
# Since multi_xml 0.9.0 the canonical constant is MultiXML; MultiXml is a
# deprecated alias (removed in v1.0) that warns on use. Prefer MultiXML so
# Grape::Xml.parse doesn't trip the deprecation, falling back to the legacy
# constant and then ActiveSupport::XmlMini.
# https://github.com/sferik/multi_xml/blob/v0.9.1/CHANGELOG.md
if defined?(::MultiXML)
Xml = ::MultiXML
elsif defined?(::MultiXml)
Xml = ::MultiXml
else
Xml = ::ActiveSupport::XmlMini
Expand Down
20 changes: 18 additions & 2 deletions spec/integration/multi_xml/xml_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# frozen_string_literal: true

describe Grape::Xml, if: defined?(MultiXml) do
subject { described_class }
# Exercise the full request stack: a Grape API parses an XML body through the
# active multi_xml backend (MultiXML on >= 0.9, the legacy MultiXml alias on
# < 0.9). Calling parse through the deprecated MultiXml constant would raise
# via the suite's deprecation handler (see spec/support/deprecated_warning_handlers.rb).
let(:app) do
Class.new(Grape::API) do
post '/request_body' do
params[:user]
end
end
end

it { is_expected.to eq(MultiXml) }
it 'parses an XML request body into params' do
env = Rack::MockRequest.env_for('/request_body', method: Rack::POST, input: '<user>Bobby T.</user>', 'CONTENT_TYPE' => 'application/xml')
response = Rack::MockResponse[*app.call(env)]

expect(response.status).to eq(201)
expect(response.body).to eq('Bobby T.')
end
end
Loading