From 0540ec5044d35fac648dcc2446bde15e179642fb Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Mon, 11 Mar 2019 12:43:40 -0700 Subject: [PATCH] Correctly handle case where a metadata key is called 'metadata' --- lib/StripeObject.php | 2 +- tests/Stripe/StripeObjectTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/StripeObject.php b/lib/StripeObject.php index a096dddef..432e95b7f 100644 --- a/lib/StripeObject.php +++ b/lib/StripeObject.php @@ -280,7 +280,7 @@ public function updateAttributes($values, $opts = null, $dirty = true) // This is necessary in case metadata is empty, as PHP arrays do // not differentiate between lists and hashes, and we consider // empty arrays to be lists. - if ($k === "metadata") { + if (($k === "metadata") && (is_array($v))) { $this->_values[$k] = StripeObject::constructFrom($v, $opts); } else { $this->_values[$k] = Util\Util::convertToStripeObject($v, $opts); diff --git a/tests/Stripe/StripeObjectTest.php b/tests/Stripe/StripeObjectTest.php index 2e841fd8d..bbf2d7c5a 100644 --- a/tests/Stripe/StripeObjectTest.php +++ b/tests/Stripe/StripeObjectTest.php @@ -473,4 +473,23 @@ public function testIsDeleted() $obj = StripeObject::constructFrom(['deleted' => true]); $this->assertTrue($obj->isDeleted()); } + + public function testDeserializeEmptyMetadata() + { + $obj = StripeObject::constructFrom([ + 'metadata' => [], + ]); + + $this->assertInstanceOf("Stripe\\StripeObject", $obj->metadata); + } + + public function testDeserializeMetadataWithKeyNamedMetadata() + { + $obj = StripeObject::constructFrom([ + 'metadata' => ['metadata' => 'value'], + ]); + + $this->assertInstanceOf("Stripe\\StripeObject", $obj->metadata); + $this->assertEquals("value", $obj->metadata->metadata); + } }