@@ -158,6 +158,113 @@ class UtilTest < Test::Unit::TestCase
158158 assert_equal [ 1 , 2 , 3 ] , obj
159159 end
160160
161+ context "#objects_to_ids" do
162+ should "convert APIResource to id" do
163+ resource = Stripe ::Charge . construct_from ( id : "ch_123" , object : "charge" )
164+ result = Util . objects_to_ids ( resource )
165+ assert_equal "ch_123" , result
166+ end
167+
168+ should "pass through primitives unchanged" do
169+ assert_equal "string" , Util . objects_to_ids ( "string" )
170+ assert_equal 123 , Util . objects_to_ids ( 123 )
171+ assert_equal true , Util . objects_to_ids ( true )
172+ end
173+
174+ should "skip nil values in hashes by default" do
175+ input = { a : "value" , b : nil , c : "another" }
176+ result = Util . objects_to_ids ( input )
177+ assert_equal ( { a : "value" , c : "another" } , result )
178+ refute result . key? ( :b )
179+ end
180+
181+ should "keep nil values in hashes when serialize_empty is true" do
182+ input = { a : "value" , b : nil , c : "another" }
183+ result = Util . objects_to_ids ( input , serialize_empty : true )
184+ assert_equal ( { a : "value" , b : nil , c : "another" } , result )
185+ assert result . key? ( :b )
186+ assert_nil result [ :b ]
187+ end
188+
189+ should "recurse on non-nil hash values" do
190+ resource = Stripe ::Charge . construct_from ( id : "ch_123" , object : "charge" )
191+ input = { charge : resource , amount : 100 }
192+ result = Util . objects_to_ids ( input )
193+ assert_equal ( { charge : "ch_123" , amount : 100 } , result )
194+ end
195+
196+ should "handle nested hashes with nil values by default" do
197+ resource = Stripe ::Charge . construct_from ( id : "ch_123" , object : "charge" )
198+ input = {
199+ charge : resource ,
200+ metadata : { key : "value" , empty : nil } ,
201+ description : nil ,
202+ }
203+ result = Util . objects_to_ids ( input )
204+ expected = {
205+ charge : "ch_123" ,
206+ metadata : { key : "value" } ,
207+ }
208+ assert_equal expected , result
209+ refute result [ :metadata ] . key? ( :empty )
210+ refute result . key? ( :description )
211+ end
212+
213+ should "handle nested hashes with nil values when serialize_empty is true" do
214+ resource = Stripe ::Charge . construct_from ( id : "ch_123" , object : "charge" )
215+ input = {
216+ charge : resource ,
217+ metadata : { key : "value" , empty : nil } ,
218+ description : nil ,
219+ }
220+ result = Util . objects_to_ids ( input , serialize_empty : true )
221+ expected = {
222+ charge : "ch_123" ,
223+ metadata : { key : "value" , empty : nil } ,
224+ description : nil ,
225+ }
226+ assert_equal expected , result
227+ assert result [ :metadata ] . key? ( :empty )
228+ assert result . key? ( :description )
229+ end
230+
231+ should "process arrays" do
232+ resource1 = Stripe ::Charge . construct_from ( id : "ch_123" , object : "charge" )
233+ resource2 = Stripe ::Charge . construct_from ( id : "ch_456" , object : "charge" )
234+ input = [ resource1 , "string" , resource2 ]
235+ result = Util . objects_to_ids ( input )
236+ assert_equal %w[ ch_123 string ch_456 ] , result
237+ end
238+
239+ should "handle complex nested structures by default" do
240+ resource = Stripe ::Charge . construct_from ( id : "ch_123" , object : "charge" )
241+ input = {
242+ charges : [ resource , nil ] ,
243+ metadata : { key : nil , nested : { value : "test" , empty : nil } } ,
244+ }
245+ result = Util . objects_to_ids ( input )
246+ expected = {
247+ charges : [ "ch_123" , nil ] ,
248+ metadata : { nested : { value : "test" } } ,
249+ }
250+ assert_equal expected , result
251+ end
252+
253+ should "handle complex nested structures when serialize_empty is true" do
254+ resource = Stripe ::Charge . construct_from ( id : "ch_123" , object : "charge" )
255+ input = {
256+ charges : [ resource , nil ] ,
257+ metadata : { key : nil , nested : { value : "test" , empty : nil } } ,
258+ }
259+ result = Util . objects_to_ids ( input , serialize_empty : true )
260+ expected = {
261+ charges : [ "ch_123" , nil ] ,
262+ metadata : { key : nil , nested : { value : "test" , empty : nil } } ,
263+ }
264+ assert_equal expected , result
265+ end
266+ end
267+
161268 context ".request_id_dashboard_url" do
162269 should "generate a livemode URL" do
163270 assert_equal "https://dashboard.stripe.com/live/logs/request-id" ,
0 commit comments