Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow strings as response without a wrapping tag. #241

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ gemfile:
- gemfiles/rails_4.0.0.gemfile
- gemfiles/rails_4.1.0.gemfile
- gemfiles/rails_4.2.0.gemfile
- gemfiles/rails_5.0.0.gemfile
- gemfiles/rails_5.0.3.gemfile
rvm:
- 2.3.0
before_install:
Expand Down
4 changes: 2 additions & 2 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ appraise "rails-4.2.0" do
end


appraise "rails-5.0.0" do
gem "rails", "5.0.0"
appraise "rails-5.0.3" do
Copy link
Contributor Author

@jesusabarca jesusabarca Aug 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumps up version of rails from 5.0.0 to 5.0.3 to include a bug fix in the minitest plugin rails/rails#29022. This only prevents the test suit from crashing. This PR will fix this #238, so it's just a temporal fix.

gem "rails", "5.0.3"
end
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class RumbasController < ApplicationController
render :soap => params[:value].to_s
end

soap_action 'single_string_response',
args: { a: :string }
return: :string
def single_string_response
render soap: params[:a].to_s, wrap_response: false
end

soap_action "concat",
:args => { :a => :string, :b => :string },
:return => :string
Expand Down Expand Up @@ -254,6 +261,7 @@ Available properties are:
* **namespace**: SOAP namespace to use. Default is `urn:WashOut`.
* **snakecase_input**: Determines if WashOut should modify parameters keys to snakecase. Default is `false`.
* **camelize_wsdl**: Determines if WashOut should camelize types within WSDL and responses. Supports `true` for CamelCase and `:lower` for camelCase. Default is `false`.
* **service_name**: Allows to define a custom name for the SOAP service. By default, the name is set as `service`.

### Camelization

Expand Down
6 changes: 5 additions & 1 deletion app/helpers/wash_out_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def wsdl_data_options(param)
end
when 'document'
{ }
else
{ }
end
end

Expand All @@ -23,10 +25,12 @@ def wsdl_data_attrs(param)
end
end

def wsdl_data(xml, params)
def wsdl_data(xml, params, wrap_response = true)
params.each do |param|
next if param.attribute?

return xml.text! param.value.to_s unless wrap_response == true

tag_name = param.name
param_options = wsdl_data_options(param)
param_options.merge! wsdl_data_attrs(param)
Expand Down
20 changes: 16 additions & 4 deletions app/views/wash_out/document/response.builder
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ xml.tag! "soap:Envelope", "xmlns:soap" => 'http://schemas.xmlsoap.org/soap/envel
"xmlns:tns" => @namespace do
if !header.nil?
xml.tag! "soap:Header" do
xml.tag! "tns:#{@action_spec[:response_tag]}" do
wsdl_data xml, header
if @response_attribute_tags.nil?
xml.tag! "#{@response_tag.nil? ? 'tns:' : @response_tag}#{@action_spec[:response_tag]}" do
wsdl_data xml, header
end
else
xml.tag! "#{@response_tag.nil? ? 'tns:' : @response_tag}#{@action_spec[:response_tag]}", @response_attribute_tags do
wsdl_data xml, header
end
end
end
end
xml.tag! "soap:Body" do
xml.tag! "tns:#{@action_spec[:response_tag]}" do
wsdl_data xml, result
if @response_attribute_tags.nil?
xml.tag! "#{@response_tag.nil? ? 'tns:' : @response_tag}#{@action_spec[:response_tag]}" do
wsdl_data xml, result
end
else
xml.tag! "#{@response_tag.nil? ? 'tns:' : @response_tag}#{@action_spec[:response_tag]}", @response_attribute_tags do
wsdl_data xml, result
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/wash_out/document/wsdl.builder
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ xml.definitions 'xmlns' => 'http://schemas.xmlsoap.org/wsdl/',
end
end

xml.service :name => "service" do
xml.service :name => @service_name do
xml.port :name => "#{@name}_port", :binding => "tns:#{@name}_binding" do
xml.tag! "soap:address", :location => WashOut::Router.url(request, @name)
end
Expand Down
20 changes: 16 additions & 4 deletions app/views/wash_out/rpc/response.builder
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ xml.tag! "soap:Envelope", "xmlns:soap" => 'http://schemas.xmlsoap.org/soap/envel
"xmlns:tns" => @namespace do
if !header.nil?
xml.tag! "soap:Header" do
xml.tag! "tns:#{@action_spec[:response_tag]}" do
wsdl_data xml, header
if @response_attribute_tags.nil?
xml.tag! "#{@response_tag.nil? ? 'tns:' : @response_tag}#{@action_spec[:response_tag]}" do
wsdl_data xml, header
end
else
xml.tag! "#{@response_tag.nil? ? 'tns:' : @response_tag}#{@action_spec[:response_tag]}", @response_attribute_tags do
wsdl_data xml, header
end
end
end
end
xml.tag! "soap:Body" do
xml.tag! "tns:#{@action_spec[:response_tag]}" do
wsdl_data xml, result
if @response_attribute_tags.nil?
xml.tag! "#{@response_tag.nil? ? 'tns:' : @response_tag}#{@action_spec[:response_tag]}" do
wsdl_data xml, result
end
else
xml.tag! "#{@response_tag.nil? ? 'tns:' : @response_tag}#{@action_spec[:response_tag]}", @response_attribute_tags do
wsdl_data xml, result
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/wash_out/rpc/wsdl.builder
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ xml.definitions 'xmlns' => 'http://schemas.xmlsoap.org/wsdl/',
end
end

xml.service :name => "service" do
xml.service :name => @service_name do
xml.port :name => "#{@name}_port", :binding => "tns:#{@name}_binding" do
xml.tag! "soap:address", :location => WashOut::Router.url(request, @name)
end
Expand Down
21 changes: 0 additions & 21 deletions gemfiles/rails_3.2.13.gemfile

This file was deleted.

2 changes: 1 addition & 1 deletion gemfiles/rails_4.0.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ gem "simplecov-summary"
gem "rails", "4.0.0"
gem "listen", "< 3.1.0"

gemspec :path => "../"
gemspec path: "../"
2 changes: 1 addition & 1 deletion gemfiles/rails_4.1.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ gem "simplecov-summary"
gem "rails", "4.1.0"
gem "listen", "< 3.1.0"

gemspec :path => "../"
gemspec path: "../"
2 changes: 1 addition & 1 deletion gemfiles/rails_4.2.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ gem "simplecov-summary"
gem "rails", "4.2.0"
gem "listen", "< 3.1.0"

gemspec :path => "../"
gemspec path: "../"
19 changes: 0 additions & 19 deletions gemfiles/rails_5.0.0.beta2.gemfile

This file was deleted.

4 changes: 2 additions & 2 deletions gemfiles/rails_5.0.0.gemfile → gemfiles/rails_5.0.3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ gem "tzinfo"
gem "pry"
gem "simplecov"
gem "simplecov-summary"
gem "rails", "5.0.0"
gem "rails", "5.0.3"

gemspec :path => "../"
gemspec path: "../"
22 changes: 15 additions & 7 deletions lib/wash_out/dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,24 @@ def _load_params(spec, xml_data)

# This action generates the WSDL for defined SOAP methods.
def _generate_wsdl
@map = self.class.soap_actions
@namespace = soap_config.namespace
@name = controller_path
@map = self.class.soap_actions
@namespace = soap_config.namespace
@response_tag = soap_config.response_tag
@response_attribute_tags = soap_config.response_attribute_tags
@name = controller_path
@service_name = soap_config.service_name

render :template => "wash_out/#{soap_config.wsdl_style}/wsdl", :layout => false,
:content_type => 'text/xml'
end

# Render a SOAP response.
def _render_soap(result, options)
@namespace = soap_config.namespace
@operation = soap_action = request.env['wash_out.soap_action']
@action_spec = self.class.soap_actions[soap_action]
@namespace = soap_config.namespace
@response_tag = soap_config.response_tag
@response_attribute_tags = soap_config.response_attribute_tags
@operation = soap_action = request.env['wash_out.soap_action']
@action_spec = self.class.soap_actions[soap_action]

result = { 'value' => result } unless result.is_a? Hash
result = HashWithIndifferentAccess.new(result)
Expand Down Expand Up @@ -137,12 +142,15 @@ def _render_soap(result, options)
header = HashWithIndifferentAccess.new(header)
end

wrap_response = options.fetch :wrap_response, true

render :template => "wash_out/#{soap_config.wsdl_style}/response",
:layout => false,
:locals => {
:header => header.present? ? inject.call(header, @action_spec[:header_out])
: nil,
:result => inject.call(result, @action_spec[:out])
:result => inject.call(result, @action_spec[:out]),
:wrap_response => wrap_response
},
:content_type => 'text/xml'
end
Expand Down
2 changes: 1 addition & 1 deletion lib/wash_out/soap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def soap_action(action, options={})

end

default_response_tag = soap_config.camelize_wsdl ? 'Response' : '_response'
default_response_tag = 'Response'
default_response_tag = action+default_response_tag


Expand Down
3 changes: 3 additions & 0 deletions lib/wash_out/soap_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class SoapConfig
wsse_password: nil,
wsse_auth_callback: nil,
soap_action_routing: true,
service_name: 'service',
response_tag: nil,
response_attribute_tags: nil
}

attr_reader :config
Expand Down
46 changes: 46 additions & 0 deletions spec/lib/wash_out_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,40 @@ def savon!(method, message={}, &block)
end
end

describe 'WSDL' do
let :wsdl do
mock_controller

HTTPI.get('http://app/route/api/wsdl').body
end

let :xml do
nori.parse wsdl
end

it "defines a default service name as 'service'" do
service_name = xml[:definitions][:service][:@name]
expect(service_name).to match 'service'
end
end

describe 'WSDL' do
let :wsdl do
mock_controller service_name: 'CustomServiceName'

HTTPI.get('http://app/route/api/wsdl').body
end

let :xml do
nori.parse wsdl
end

it 'allows to define a custom service name' do
service_name = xml[:definitions][:service][:@name]
expect(service_name).to match 'CustomServiceName'
end
end

describe "Dispatcher" do

context "simple actions" do
Expand Down Expand Up @@ -270,6 +304,18 @@ def funky

expect(savon(:funky, :a => 42, :b => 'k')[:funky_response][:value]).to eq '420k'
end

it 'responds with single strings without wraping the response' do
mock_controller do
soap_action 'single_string', args: { a: :string }, return: :string
def single_string
render soap: params[:a], wrap_response: false
end
end

response = savon(:single_string, a: 'single string')[:single_string_response]
expect(response).to match 'single string'
end
end

context "complex actions" do
Expand Down