Skip to content

Commit

Permalink
Fix: fields that are set to 0 are erroneously removed from request pa…
Browse files Browse the repository at this point in the history
…yload (#370)

* initial commit: remove array filtering for params and add unit test

* Empty commit

* grammar

* changelog

* changelog date

* changelog semvem version

* whitespacing

* Update tests/Clients/BaseRestResourceTest.php

Co-authored-by: Liz Kenyon <[email protected]>

* fix changelog, move PR to unreleased

* update array filter to only filter out empty strings

---------

Co-authored-by: Liz Kenyon <[email protected]>
  • Loading branch information
andy-liuu and lizkenyon authored Sep 24, 2024
1 parent 18d1d3f commit 50aeb6a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
- [#370](https://github.com/Shopify/shopify-api-php/pull/370) [Patch] Fix params set to zero being removed from request payload

## v5.6.1 - 2024-09-06

Expand Down
5 changes: 4 additions & 1 deletion src/Rest/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ protected static function request(

$client = new Rest($session->getShop(), $session->getAccessToken());

$params = array_filter($params);
$params = array_filter($params, function ($value) {
return $value !== "";
});

switch ($httpMethod) {
case "get":
$response = $client->get(
Expand Down
18 changes: 18 additions & 0 deletions tests/Clients/BaseRestResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ public function testFindsWithParam()
$this->assertEquals([1, "attribute"], [$resource->id, $resource->attribute]);
}

public function testFindsWithFalseyParams()
{
$body = ["fake_resource" => ["id" => 1, "attribute" => "attribute"]];

$this->mockTransportRequests([
new MockRequest(
$this->buildMockHttpResponse(200, $body),
"{$this->prefix}/fake_resources/1.json?zero_param=0",
"GET",
null,
["X-Shopify-Access-Token: access-token"],
),
]);

$resource = FakeResource::find($this->session, 1, ["zero_param" => "0", "empty_param" => ""]);
$this->assertEquals([1, "attribute"], [$resource->id, $resource->attribute]);
}

public function testFindsResourceAndChildrenById()
{
$body = ["fake_resource" => [
Expand Down

0 comments on commit 50aeb6a

Please sign in to comment.