Skip to content

Commit

Permalink
fix for namespaced entries in SOAP response XML. closes savonrb#87.
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiii committed Oct 16, 2010
1 parent 36d77cc commit 0c3aa5e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
Both Savon::SOAP::Fault and Savon::HTTP::Error now contain the HTTPI::Response.
They also inherit from Savon::Error, making it easier to rescue both at the same time.

* Fix for issue #87 (Namespaced entries in the xml).
Thanks to Leonardo Borges.

* Fix for issue #81 (irb on Ruby 1.9.2 doesn't disable wsdl).
Replaced Savon::WSDL::Document#to_s with a #to_xml method.

Expand Down
9 changes: 8 additions & 1 deletion lib/savon/core_ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ def map_soap_response
when ::String then value.map_soap_response
end

hash.merge key.strip_namespace.snakecase.to_sym => value
no_ns_key = key.strip_namespace.snakecase.to_sym
if hash[no_ns_key] # key already exists, value should be added as an Array
hash[no_ns_key] = [hash[no_ns_key], value].flatten
result = hash
else
result = hash.merge no_ns_key => value
end
result
end
end

Expand Down
52 changes: 26 additions & 26 deletions spec/fixtures/response/response_fixture.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
class ResponseFixture
class << self

def self.authentication(value = nil)
case value
when :to_hash
{ :success => true,
:authentication_value => {
:token => "a68d1d6379b62ff339a0e0c69ed4d9cf",
:token_hash => "AAAJxA;cIedoT;mY10ExZwG6JuKgp2OYKxow==",
:client => "radclient"
}
}
else
@@authentication ||= load_fixture :authentication
def authentication(value = nil)
@authentication ||= load_fixture :authentication

case value
when :to_hash then Savon::SOAP::XML.to_hash(@authentication)[:authenticate_response][:return]
else @authentication
end
end
end

def self.soap_fault
@@soap_fault ||= load_fixture :soap_fault
end
def soap_fault
@soap_fault ||= load_fixture :soap_fault
end

def self.soap_fault12
@@soap_fault12 ||= load_fixture :soap_fault12
end
def soap_fault12
@soap_fault12 ||= load_fixture :soap_fault12
end

def self.multi_ref
@@multi_ref ||= load_fixture :multi_ref
end
def multi_ref
@multi_ref ||= load_fixture :multi_ref
end

private
def list
@list ||= load_fixture :list
end

def self.load_fixture(fixture)
File.read File.dirname(__FILE__) + "/xml/#{fixture}.xml"
end
private

def load_fixture(fixture)
File.read File.dirname(__FILE__) + "/xml/#{fixture}.xml"
end

end
end
18 changes: 18 additions & 0 deletions spec/fixtures/response/xml/list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<MultiNamespacedEntryResponse xmlns="http://www.example.com/BusinessRulesEngine/xsd">
<history>
<ns10:case xmlns:ns10="http://www.example.com/Common/xsd">
<ns10:logTime>2010-09-21T18:22:01.558+10:00</ns10:logTime>
<ns10:logType>Notes Log</ns10:logType>
<ns10:logText>test</ns10:logText>
</ns10:case>
<ns11:case xmlns:ns11="http://www.example.com/Common/xsd">
<ns11:logTime>2010-09-21T18:22:07.038+10:00</ns11:logTime>
<ns11:logType>Notes Log</ns11:logType>
<ns11:logText>another test</ns11:logText>
</ns11:case>
</history>
</MultiNamespacedEntryResponse>
</soapenv:Body>
</soapenv:Envelope>
17 changes: 17 additions & 0 deletions spec/savon/core_ext/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ def singleton.to_datetime

soap_response.map_soap_response.should == result
end

it "should convert namespaced entries to array elements" do
soap_response = {
"history" => {
"ns10:case" => { "ns10:name" => "a_name" },
"ns11:case" => { "ns11:name" => "another_name" }
}
}

result = {
:history => {
:case => [{ :name => "a_name" }, { :name => "another_name" }]
}
}

soap_response.map_soap_response.should == result
end
end

end
7 changes: 7 additions & 0 deletions spec/savon/soap/xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
hash[:list_response].should be_a(Hash)
hash[:multi_ref].should be_an(Array)
end

it "should add existing namespaced elements as an array" do
hash = Savon::SOAP::XML.to_hash ResponseFixture.list

hash[:multi_namespaced_entry_response][:history].should be_a(Hash)
hash[:multi_namespaced_entry_response][:history][:case].should be_an(Array)
end
end

describe ".new" do
Expand Down

0 comments on commit 0c3aa5e

Please sign in to comment.