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

Nil values serialization #6

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions lib/restforce/bulk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "restforce/bulk/version"
require "active_support/inflector"
require "zip"
require "securerandom"

module Restforce
module Bulk
Expand Down
8 changes: 6 additions & 2 deletions lib/restforce/bulk/builder/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def generate(data)
data.each do |item|
xml.sObject do
item.each do |attr, value|
xml.send(attr, value)
xml.send(attr, value, value.nil? ? {"xsi:nil" => true} : {})
end
end
end
Expand All @@ -52,7 +52,11 @@ def generate(data)

def build_xml(root, options={}, &block)
Nokogiri::XML::Builder.new { |xml|
xml.send(root, { xmlns: 'http://www.force.com/2009/06/asyncapi/dataload' }.merge(options), &block)
namespaces = {
"xmlns" => 'http://www.force.com/2009/06/asyncapi/dataload',
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance"
}
xml.send(root, namespaces.merge(options), &block)
}.to_xml(encoding: 'UTF-8')
end
end
Expand Down
32 changes: 32 additions & 0 deletions spec/restforce/bulk/builder/xml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'ostruct'
require "spec_helper"

describe Restforce::Bulk::Builder::Xml, mock_restforce: true do
let(:builder) { Restforce::Bulk::Builder::Xml.new(:upsert) }

def xml(field, value, tag = {})
build_bulk_xml(:sObjects) do |builder|
builder.sObject {
builder.send(field, value, tag)
}
end
end

it "adds a nil=true tag to nil values" do
payload = builder.generate([{IsActive: nil}])

expect(payload).to eq(xml(:IsActive, nil, "xsi:nil" => true))
end

it "generates non-nil values" do
payload = builder.generate([{IsActive: true}])

expect(payload).to eq(xml(:IsActive, true))
end

it "generates false values" do
payload = builder.generate([{IsActive: false}])

expect(payload).to eq(xml(:IsActive, false))
end
end
6 changes: 5 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def expect_restforce_request(method, path, data=nil, content_type=:xml, headers=
end

def build_bulk_xml(root, options={}, &block)
namespaces = {
"xmlns" => 'http://www.force.com/2009/06/asyncapi/dataload',
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance"
}
Nokogiri::XML::Builder.new do |xml|
xml.send(root, { xmlns: 'http://www.force.com/2009/06/asyncapi/dataload' }.merge(options), &block)
xml.send(root, namespaces.merge(options), &block)
end.to_xml(encoding: 'UTF-8')
end

Expand Down