@@ -269,5 +269,77 @@ def initialize(games: nil)
269269 assert_equal expected , params . to_h
270270 end
271271 end
272+
273+ context "V2 RequestParams explicit key tracking" do
274+ should "only serialize explicitly set fields for V2 classes" do
275+ params = Stripe ::V2 ::Billing ::MeterEventCreateParams . new (
276+ event_name : "my_event" ,
277+ payload : { stripe_customer_id : "cus_123" , value : "25" }
278+ )
279+
280+ result = params . to_h
281+
282+ assert_equal "my_event" , result [ :event_name ]
283+ assert_equal ( { stripe_customer_id : "cus_123" , value : "25" } , result [ :payload ] )
284+ refute result . key? ( :identifier ) , "identifier was not set and should not be in the hash"
285+ refute result . key? ( :timestamp ) , "timestamp was not set and should not be in the hash"
286+ end
287+
288+ should "serialize fields explicitly set to nil for V2 classes" do
289+ params = Stripe ::V2 ::Billing ::MeterEventCreateParams . new (
290+ event_name : "my_event" ,
291+ identifier : nil ,
292+ payload : { stripe_customer_id : "cus_123" , value : "25" }
293+ )
294+
295+ result = params . to_h
296+
297+ assert_equal "my_event" , result [ :event_name ]
298+ assert result . key? ( :identifier ) , "identifier was explicitly set to nil and should be in the hash"
299+ assert_nil result [ :identifier ]
300+ assert_equal ( { stripe_customer_id : "cus_123" , value : "25" } , result [ :payload ] )
301+ refute result . key? ( :timestamp ) , "timestamp was not set and should not be in the hash"
302+ end
303+
304+ should "serialize fields explicitly set after initialization for V2 classes" do
305+ params = Stripe ::V2 ::Billing ::MeterEventCreateParams . new (
306+ event_name : "my_event" ,
307+ payload : { stripe_customer_id : "cus_123" , value : "25" }
308+ )
309+
310+ params . identifier = "Something"
311+ params . timestamp = nil
312+
313+ result = params . to_h
314+
315+ assert_equal "my_event" , result [ :event_name ]
316+ assert_equal "Something" , result [ :identifier ]
317+ assert_equal ( { stripe_customer_id : "cus_123" , value : "25" } , result [ :payload ] )
318+ assert result . key? ( :timestamp )
319+ assert_nil result [ :timestamp ]
320+ end
321+
322+ should "serialize all fields even if some fields are set after initialization for V1 classes" do
323+ params = FooCreateParams . new
324+
325+ params . fun = "games"
326+
327+ result = params . to_h
328+
329+ assert_equal "games" , result [ :fun ]
330+ assert result . key? ( :team ) , "V1 classes should include all fields even if not explicitly set"
331+ assert_nil result [ :team ]
332+ end
333+
334+ should "serialize all fields including nil defaults for V1 classes" do
335+ params = FooCreateParams . new ( fun : "games" )
336+
337+ result = params . to_h
338+
339+ assert_equal "games" , result [ :fun ]
340+ assert result . key? ( :team ) , "V1 classes should include all fields even if not explicitly set"
341+ assert_nil result [ :team ]
342+ end
343+ end
272344 end
273345end
0 commit comments