Skip to content

Commit

Permalink
Merge pull request #1602 from stripe/richardm-fix-normalizing-non-str…
Browse files Browse the repository at this point in the history
…ing-ids

Fix crashes when "id" is not a string.
  • Loading branch information
richardm-stripe authored Nov 6, 2023
2 parents e0a960c + 97d8649 commit 87a44c0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ public static function urlEncode($key)
public static function normalizeId($id)
{
if (\is_array($id)) {
// see https://github.com/stripe/stripe-php/pull/1602
if (!isset($id['id'])) {
return [null, $id];
}
$params = $id;
$id = $params['id'];
unset($params['id']);
Expand Down
34 changes: 34 additions & 0 deletions tests/Stripe/StripeObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ final class StripeObjectTest extends \Stripe\TestCase
/** @var \ReflectionProperty */
private $optsReflector;

/** @var \ReflectionProperty */
private $retrieveOptionsReflector;

/**
* @before
*/
Expand All @@ -31,6 +34,10 @@ public function setUpReflectors()
// This is used to access the `_opts` protected variable
$this->optsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_opts');
$this->optsReflector->setAccessible(true);

// This is used to access the `_retrieveOptions` protected variable
$this->retrieveOptionsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_retrieveOptions');
$this->retrieveOptionsReflector->setAccessible(true);
}

public function testArrayAccessorsSemantics()
Expand Down Expand Up @@ -540,6 +547,33 @@ public function testIsDeleted()
static::assertTrue($obj->isDeleted());
}

public function testConstructorIdPassing()
{
$obj = new StripeObject(['id' => 'id_foo', 'other' => 'bar']);
static::assertSame('id_foo', $obj->id);
static::assertSame(['other' => 'bar'], $this->retrieveOptionsReflector->getValue($obj));

$obj = new StripeObject('id_foo');
static::assertSame('id_foo', $obj->id);
static::assertSame([], $this->retrieveOptionsReflector->getValue($obj));

$obj = new StripeObject(['id' => 'id_foo']);
static::assertSame('id_foo', $obj->id);
static::assertSame([], $this->retrieveOptionsReflector->getValue($obj));

$obj = new StripeObject(['id' => ['foo' => 'bar']]);
static::assertSame(['foo' => 'bar'], $obj->id);
static::assertSame([], $this->retrieveOptionsReflector->getValue($obj));
}

public function testConstructFromIdPassing()
{
$obj = StripeObject::constructFrom(['inner' => ['id' => ['foo' => 'bar']]]);

static::assertSame(['foo' => 'bar'], $obj['inner']->id->toArray());
static::assertSame([], $this->retrieveOptionsReflector->getValue($obj));
}

public function testDeserializeEmptyMetadata()
{
/** @var mixed $obj */
Expand Down

0 comments on commit 87a44c0

Please sign in to comment.