Ticket #8537: parse_xml_arrays_to_ruby_arrays.diff
| File parse_xml_arrays_to_ruby_arrays.diff, 12.1 kB (added by hasmanyjosh, 2 years ago) |
|---|
-
activeresource/test/connection_test.rb
old new 115 115 116 116 def test_get_collection_empty 117 117 people = @conn.get("/people_empty_elements.xml") 118 assert_ nilpeople118 assert_equal [], people 119 119 end 120 120 121 121 def test_post -
activeresource/test/base/custom_methods_test.rb
old new 6 6 def setup 7 7 @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') 8 8 @matz_deep = { :id => 1, :name => 'Matz', :other => 'other' }.to_xml(:root => 'person') 9 @matz_array = [{ :id => 1, :name => 'Matz' }].to_xml(:root => 'people') 9 10 @ryan = { :name => 'Ryan' }.to_xml(:root => 'person') 10 11 @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address') 11 12 @addy_deep = { :id => 1, :street => '12345 Street', :zip => "27519" }.to_xml(:root => 'address') … … 15 16 mock.get "/people/1.xml", {}, @matz 16 17 mock.get "/people/1/shallow.xml", {}, @matz 17 18 mock.get "/people/1/deep.xml", {}, @matz_deep 18 mock.get "/people/retrieve.xml?name=Matz", {}, "<people>#{@matz}</people>"19 mock.get "/people/managers.xml", {}, "<people>#{@matz}</people>"19 mock.get "/people/retrieve.xml?name=Matz", {}, @matz_array 20 mock.get "/people/managers.xml", {}, @matz_array 20 21 mock.put "/people/1/promote.xml?position=Manager", {}, nil, 204 21 22 mock.put "/people/promote.xml?name=Matz", {}, nil, 204, {} 22 23 mock.put "/people/sort.xml?by=name", {}, nil, 204 -
activeresource/test/base_test.rb
old new 9 9 @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') 10 10 @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address') 11 11 @default_request_headers = { 'Content-Type' => 'application/xml' } 12 @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") 13 @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people') 14 @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people') 15 @addresses = [{ :id => 1, :street => '12345 Street' }].to_xml(:root => 'addresses') 12 16 13 17 ActiveResource::HttpMock.respond_to do |mock| 14 18 mock.get "/people/1.xml", {}, @matz … … 18 22 mock.delete "/people/1.xml", {}, nil, 200 19 23 mock.delete "/people/2.xml", {}, nil, 400 20 24 mock.get "/people/99.xml", {}, nil, 404 21 mock.post "/people.xml", {}, "<person><name>Rick</name><age type='integer'>25</age></person>", 201, 'Location' => '/people/5.xml'22 mock.get "/people.xml", {}, "<people>#{@matz}#{@david}</people>"23 mock.get "/people/1/addresses.xml", {}, "<addresses>#{@addy}</addresses>"25 mock.post "/people.xml", {}, @rick, 201, 'Location' => '/people/5.xml' 26 mock.get "/people.xml", {}, @people 27 mock.get "/people/1/addresses.xml", {}, @addresses 24 28 mock.get "/people/1/addresses/1.xml", {}, @addy 25 29 mock.get "/people/1/addresses/2.xml", {}, nil, 404 26 30 mock.get "/people/2/addresses/1.xml", {}, nil, 404 … … 225 229 end 226 230 227 231 def test_find_all_by_from 228 ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, "<people>#{@david}</people>"}232 ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, @people_david } 229 233 230 234 people = Person.find(:all, :from => "/companies/1/people.xml") 231 235 assert_equal 1, people.size … … 233 237 end 234 238 235 239 def test_find_all_by_from_with_options 236 ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, "<people>#{@david}</people>"}240 ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, @people_david } 237 241 238 242 people = Person.find(:all, :from => "/companies/1/people.xml") 239 243 assert_equal 1, people.size … … 241 245 end 242 246 243 247 def test_find_all_by_symbol_from 244 ActiveResource::HttpMock.respond_to { |m| m.get "/people/managers.xml", {}, "<people>#{@david}</people>"}248 ActiveResource::HttpMock.respond_to { |m| m.get "/people/managers.xml", {}, @people_david } 245 249 246 250 people = Person.find(:all, :from => :managers) 247 251 assert_equal 1, people.size -
activeresource/lib/active_resource/connection.rb
old new 81 81 end 82 82 83 83 def xml_from_response(response) 84 if response = from_xml_data(Hash.from_xml(response.body)) 85 response.first 86 else 87 nil 88 end 84 from_xml_data(Hash.from_xml(response.body)) 89 85 end 90 86 91 87 … … 150 146 # Manipulate from_xml Hash, because xml_simple is not exactly what we 151 147 # want for ActiveResource. 152 148 def from_xml_data(data) 153 case data 154 when Hash 155 if data.keys.size == 1 156 case data.values.first 157 when Hash then [ from_xml_data(data.values.first) ] 158 when Array then from_xml_data(data.values.first) 159 else data.values.first 160 end 161 else 162 data.each_key { |key| data[key] = from_xml_data(data[key]) } 163 data 164 end 165 when Array then data.collect { |val| from_xml_data(val) } 166 else data 149 if data.is_a?(Hash) && data.keys.size == 1 150 data.values.first 151 else 152 data 167 153 end 168 154 end 155 169 156 end 170 157 end -
activesupport/test/core_ext/array_ext_test.rb
old new 121 121 { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') } 122 122 ].to_xml(:skip_instruct => true, :indent => 0) 123 123 124 assert_equal "<records><record>", xml.first(17), xml124 assert_equal '<records type="array"><record>', xml.first(30) 125 125 assert xml.include?(%(<age type="integer">26</age>)), xml 126 126 assert xml.include?(%(<age-in-millis type="integer">820497600000</age-in-millis>)), xml 127 127 assert xml.include?(%(<name>David</name>)), xml … … 135 135 { :name => "David", :age => 26, :age_in_millis => 820497600000 }, { :name => "Jason", :age => 31 } 136 136 ].to_xml(:skip_instruct => true, :indent => 0, :root => "people") 137 137 138 assert_equal "<people><person>", xml.first(16)138 assert_equal '<people type="array"><person>', xml.first(29) 139 139 end 140 140 141 141 def test_to_xml_with_options -
activesupport/test/core_ext/hash_ext_test.rb
old new 370 370 def test_two_levels_with_array 371 371 xml = { :name => "David", :addresses => [{ :street => "Paulina" }, { :street => "Evergreen" }] }.to_xml(@xml_options) 372 372 assert_equal "<person>", xml.first(8) 373 assert xml.include?(%(<addresses ><address>))373 assert xml.include?(%(<addresses type="array"><address>)) 374 374 assert xml.include?(%(<address><street>Paulina</street></address>)) 375 375 assert xml.include?(%(<address><street>Evergreen</street></address>)) 376 376 assert xml.include?(%(<name>David</name>)) … … 378 378 379 379 def test_three_levels_with_array 380 380 xml = { :name => "David", :addresses => [{ :streets => [ { :name => "Paulina" }, { :name => "Paulina" } ] } ] }.to_xml(@xml_options) 381 assert xml.include?(%(<addresses ><address><streets><street><name>))381 assert xml.include?(%(<addresses type="array"><address><streets type="array"><street><name>)) 382 382 end 383 383 384 384 def test_single_record_from_xml … … 447 447 448 448 def test_multiple_records_from_xml 449 449 topics_xml = <<-EOT 450 <topics >450 <topics type="array"> 451 451 <topic> 452 452 <title>The First Topic</title> 453 453 <author-name>David</author-name> … … 491 491 :parent_id => nil 492 492 }.stringify_keys 493 493 494 assert_equal expected_topic_hash, Hash.from_xml(topics_xml)["topics"] ["topic"].first494 assert_equal expected_topic_hash, Hash.from_xml(topics_xml)["topics"].first 495 495 end 496 496 497 497 def test_single_record_from_xml_with_attributes_other_than_type … … 516 516 517 517 assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["rsp"]["photos"]["photo"] 518 518 end 519 520 def test_empty_array_from_xml 521 blog_xml = <<-XML 522 <blog> 523 <posts type="array"></posts> 524 </blog> 525 XML 526 expected_blog_hash = {"blog" => {"posts" => []}} 527 assert_equal expected_blog_hash, Hash.from_xml(blog_xml) 528 end 519 529 530 def test_array_with_one_entry_from_xml 531 blog_xml = <<-XML 532 <blog> 533 <posts type="array"> 534 <post>a post</post> 535 </posts> 536 </blog> 537 XML 538 expected_blog_hash = {"blog" => {"posts" => ["a post"]}} 539 assert_equal expected_blog_hash, Hash.from_xml(blog_xml) 540 end 541 542 def test_array_with_multiple_entries_from_xml 543 blog_xml = <<-XML 544 <blog> 545 <posts type="array"> 546 <post>a post</post> 547 <post>another post</post> 548 </posts> 549 </blog> 550 XML 551 expected_blog_hash = {"blog" => {"posts" => ["a post", "another post"]}} 552 assert_equal expected_blog_hash, Hash.from_xml(blog_xml) 553 end 554 520 555 def test_xsd_like_types_from_xml 521 556 bacon_xml = <<-EOT 522 557 <bacon> -
activesupport/lib/active_support/core_ext/hash/conversions.rb
old new 163 163 else 164 164 content 165 165 end 166 elsif value['type'] == 'array' 167 child_key, entries = value.detect { |k,v| k != 'type' } # child_key is throwaway 168 if entries.nil? 169 [] 170 else 171 case entries.class.to_s # something weird with classes not matching here. maybe singleton methods breaking is_a? 172 when "Array" 173 entries.collect { |v| typecast_xml_value(v) } 174 when "Hash" 175 [typecast_xml_value(entries)] 176 else 177 raise "can't typecast #{entries.inspect}" 178 end 179 end 166 180 elsif value['type'] == 'string' && value['nil'] != 'true' 167 181 "" 168 182 else -
activesupport/lib/active_support/core_ext/array/conversions.rb
old new 63 63 64 64 opts = options.merge({ :root => children }) 65 65 66 options[:builder].tag!(root) { 67 yield options[:builder] if block_given? 68 each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) } 69 } 66 xml = options[:builder] 67 if empty? 68 xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) 69 else 70 xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) { 71 yield xml if block_given? 72 each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) } 73 } 74 end 70 75 end 71 76 72 77 end