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

Services bugfix: convert nested null params to empty strings #933

Merged
merged 7 commits into from
May 15, 2020
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
7 changes: 5 additions & 2 deletions lib/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ public function getClient()
*
* @param null|array $params
*/
private static function formatParams($params)
public static function formatParams($params)
{
if (null === $params) {
return $params;
}

$formatted = [];
foreach ($params as $k => $v) {
if (null === $v) {
if (\is_array($v)) {
$formatted[$k] = static::formatParams($v);
} elseif (null === $v) {
$formatted[$k] = '';
} else {
$formatted[$k] = $v;
Expand Down
18 changes: 18 additions & 0 deletions tests/Stripe/Service/AbstractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,22 @@ public function testRetrieveThrowsIfIdNullIsWhitespace()

$this->service->retrieve(' ');
}

public function testFormatParams()
{
$result = \Stripe\Service\AbstractService::formatParams(['foo' => null]);
static::assertTrue('' === $result['foo']);
static::assertTrue(null !== $result['foo']);

$result = \Stripe\Service\AbstractService::formatParams(['foo' => ['bar' => null, 'baz' => 1]]);
static::assertTrue('' === $result['foo']['bar']);
static::assertTrue(null !== $result['foo']['bar']);
static::assertTrue(1 === $result['foo']['baz']);

$result = \Stripe\Service\AbstractService::formatParams(['foo' => ["bar", null, null, "baz"]]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about passing

[
  'email' => null,
  'invoice_settings' => [
    'default_payment_method' => 'null',
  ],
]

As in, this method didn't seem recurring or able to handle non top-level params

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite follow -- this PR adds a recursive call https://github.com/stripe/stripe-php/pull/933/files#diff-d6b577df0148860a08e6dfb3025bd0d0R52

and this test case covers non top-level params -- everything is nested one level under "foo".

static::assertTrue('bar' === $result['foo'][0]);
static::assertTrue('' === $result['foo'][1]);
static::assertTrue('' === $result['foo'][2]);
static::assertTrue('baz' === $result['foo'][3]);
}
}