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

Fix crashes when "id" is not a string. #1602

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ public static function urlEncode($key)
public static function normalizeId($id)
{
if (\is_array($id)) {
if (!isset($id['id'])) {
richardm-stripe marked this conversation as resolved.
Show resolved Hide resolved
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
Loading