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

Namespaced entries in the xml #87

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions lib/savon/core_ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ def map_soap_response
when Array then value.map { |val| val.map_soap_response rescue val }
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] #existing key 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
6 changes: 5 additions & 1 deletion spec/fixtures/response/response_fixture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def self.soap_fault12
end

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

def self.list_xml
@@list_xml ||= load_fixture :list
end

private
Expand Down
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>
21 changes: 19 additions & 2 deletions spec/savon/core_ext/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def singleton.to_datetime
result = { :user_response => { :account_status => "active" } }

soap_response.map_soap_response.should == result
end

end
it "should strip namespaces from Hash keys" do
soap_response = { "ns:userResponse" => { "ns2:id" => "666" } }
result = { :user_response => { :id => "666" } }
Expand Down 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/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@
@response.http.should respond_to(:message)
@response.http.should respond_to(:body)
end

it "should add existing namespaced elements as an array" do
@response = Savon::Response.new http_response_mock(200, ResponseFixture.list_xml, "OK")

@response.to_hash[:multi_namespaced_entry_response][:history].should be_a(Hash)
@response.to_hash[:multi_namespaced_entry_response][:history][:case].should be_an(Array)
end

describe "GZipped responses" do
it "should be decoded if Content-encoding header is gzip" do
Expand Down