From 75e2cdd33f6f57a67bdae1bd77470b347177f85b Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Thu, 22 Oct 2026 10:02:34 +0100 Subject: [PATCH 1/4] fix: send an empty JSON object for operations without a body runOperation() set "Content-Type: application/json" but sent no body when $body was empty, e.g. for activate, merge, pause, resume, redeploy and deactivate. Send "{}" instead, so the request is valid JSON as the API spec requires. GET operations still send no body. Co-Authored-By: Claude Opus 5.5 --- src/Model/ApiResourceBase.php | 4 ++- tests/ResourceTest.php | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/Model/ApiResourceBase.php b/src/Model/ApiResourceBase.php index a339b104..00b31d6b 100644 --- a/src/Model/ApiResourceBase.php +++ b/src/Model/ApiResourceBase.php @@ -303,9 +303,11 @@ public function runOperation(string $op, string $method = 'POST', array $body = if (! $this->operationAvailable($op, true)) { throw new OperationUnavailableException("Operation not available: {$op}"); } + // Send an empty JSON object rather than no body, except for GET requests. + $encoded = strtoupper($method) === 'GET' ? null : ($body ? \json_encode($body) : '{}'); $request = new Request($method, $this->getLink("#{$op}"), [ 'Content-Type' => 'application/json', - ], $body ? \json_encode($body) : null); + ], $encoded); $data = $this->send($request, $this->client); return new Result($data, $this->baseUrl, $this->client, static::class); diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index df58f5fe..089b7f9d 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -4,6 +4,12 @@ namespace Platformsh\Client\Tests; +use GuzzleHttp\Client; +use GuzzleHttp\Handler\MockHandler; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; +use GuzzleHttp\Psr7\Response; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Platformsh\Client\Model\ApiResourceBase; @@ -95,4 +101,46 @@ public function testInvalidPropertiesBlockUpdate() 'testProperty' => 2, ]); } + + public static function operationBodyProvider(): array + { + return [ + 'POST without a body' => ['POST', [], '{}'], + 'POST with a body' => ['post', [ + 'foo' => 'bar', + ], '{"foo":"bar"}'], + 'PATCH without a body' => ['PATCH', [], '{}'], + 'GET' => ['GET', [], ''], + 'lowercase GET' => ['get', [], ''], + ]; + } + + /** + * Test the request body sent by Resource::runOperation(). + */ + #[DataProvider('operationBodyProvider')] + public function testRunOperationBody(string $method, array $body, string $expected): void + { + $history = []; + $stack = HandlerStack::create(new MockHandler([new Response(200)])); + $stack->push(Middleware::history($history)); + $resource = new MockApiResource([ + '_links' => [ + 'self' => [ + 'href' => 'https://example.com/resources/test-id', + ], + '#operate' => [ + 'href' => '/resources/test-id/operate', + ], + ], + ], 'https://example.com/', new Client([ + 'handler' => $stack, + ])); + + $resource->runOperation('operate', $method, $body); + + $this->assertCount(1, $history); + $this->assertSame(strtoupper($method), $history[0]['request']->getMethod()); + $this->assertSame($expected, (string) $history[0]['request']->getBody()); + } } From fcae10f7d7c52f67a996480d0d5b2d97a4d5e835 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Thu, 22 Oct 2026 10:11:26 +0100 Subject: [PATCH 2/4] fix: only send an empty JSON object for POST, PUT and PATCH Keep the body of GET operations that pass data, and send no body for other methods without one (e.g. DELETE), as before. Co-Authored-By: Claude Opus 5.5 --- src/Model/ApiResourceBase.php | 4 ++-- tests/ResourceTest.php | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Model/ApiResourceBase.php b/src/Model/ApiResourceBase.php index 00b31d6b..3e2638cf 100644 --- a/src/Model/ApiResourceBase.php +++ b/src/Model/ApiResourceBase.php @@ -303,8 +303,8 @@ public function runOperation(string $op, string $method = 'POST', array $body = if (! $this->operationAvailable($op, true)) { throw new OperationUnavailableException("Operation not available: {$op}"); } - // Send an empty JSON object rather than no body, except for GET requests. - $encoded = strtoupper($method) === 'GET' ? null : ($body ? \json_encode($body) : '{}'); + // Send an empty JSON object rather than no body, for methods that expect one. + $encoded = $body ? \json_encode($body) : (in_array(strtoupper($method), ['POST', 'PUT', 'PATCH'], true) ? '{}' : null); $request = new Request($method, $this->getLink("#{$op}"), [ 'Content-Type' => 'application/json', ], $encoded); diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index 089b7f9d..cf2a8afe 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -112,6 +112,10 @@ public static function operationBodyProvider(): array 'PATCH without a body' => ['PATCH', [], '{}'], 'GET' => ['GET', [], ''], 'lowercase GET' => ['get', [], ''], + 'GET with a body' => ['GET', [ + 'foo' => 'bar', + ], '{"foo":"bar"}'], + 'DELETE' => ['DELETE', [], ''], ]; } From 78cc4bc6e05c97e54e7b36fa86550faf27adeb7d Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Thu, 22 Oct 2026 10:15:41 +0100 Subject: [PATCH 3/4] fix: only send an empty JSON object for POST The operations that need a body are all POST requests. Co-Authored-By: Claude Opus 5.5 --- src/Model/ApiResourceBase.php | 4 ++-- tests/ResourceTest.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Model/ApiResourceBase.php b/src/Model/ApiResourceBase.php index 3e2638cf..334954c6 100644 --- a/src/Model/ApiResourceBase.php +++ b/src/Model/ApiResourceBase.php @@ -303,8 +303,8 @@ public function runOperation(string $op, string $method = 'POST', array $body = if (! $this->operationAvailable($op, true)) { throw new OperationUnavailableException("Operation not available: {$op}"); } - // Send an empty JSON object rather than no body, for methods that expect one. - $encoded = $body ? \json_encode($body) : (in_array(strtoupper($method), ['POST', 'PUT', 'PATCH'], true) ? '{}' : null); + // Send an empty JSON object rather than no body, for POST requests. + $encoded = $body ? \json_encode($body) : (strtoupper($method) === 'POST' ? '{}' : null); $request = new Request($method, $this->getLink("#{$op}"), [ 'Content-Type' => 'application/json', ], $encoded); diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index cf2a8afe..cad42e2b 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -109,7 +109,8 @@ public static function operationBodyProvider(): array 'POST with a body' => ['post', [ 'foo' => 'bar', ], '{"foo":"bar"}'], - 'PATCH without a body' => ['PATCH', [], '{}'], + 'lowercase POST' => ['post', [], '{}'], + 'PATCH without a body' => ['PATCH', [], ''], 'GET' => ['GET', [], ''], 'lowercase GET' => ['get', [], ''], 'GET with a body' => ['GET', [ From a2c18346f281e521cfe0aba1359ddd29a625c2ad Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Thu, 22 Oct 2026 10:16:13 +0100 Subject: [PATCH 4/4] fix: also send an empty JSON object for PATCH So update([]) sends a valid (empty) merge patch. Co-Authored-By: Claude Opus 5.5 --- src/Model/ApiResourceBase.php | 4 ++-- tests/ResourceTest.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Model/ApiResourceBase.php b/src/Model/ApiResourceBase.php index 334954c6..1aa5f205 100644 --- a/src/Model/ApiResourceBase.php +++ b/src/Model/ApiResourceBase.php @@ -303,8 +303,8 @@ public function runOperation(string $op, string $method = 'POST', array $body = if (! $this->operationAvailable($op, true)) { throw new OperationUnavailableException("Operation not available: {$op}"); } - // Send an empty JSON object rather than no body, for POST requests. - $encoded = $body ? \json_encode($body) : (strtoupper($method) === 'POST' ? '{}' : null); + // Send an empty JSON object rather than no body, for POST and PATCH requests. + $encoded = $body ? \json_encode($body) : (in_array(strtoupper($method), ['POST', 'PATCH'], true) ? '{}' : null); $request = new Request($method, $this->getLink("#{$op}"), [ 'Content-Type' => 'application/json', ], $encoded); diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index cad42e2b..85c5b67f 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -110,7 +110,8 @@ public static function operationBodyProvider(): array 'foo' => 'bar', ], '{"foo":"bar"}'], 'lowercase POST' => ['post', [], '{}'], - 'PATCH without a body' => ['PATCH', [], ''], + 'PATCH without a body' => ['PATCH', [], '{}'], + 'PUT without a body' => ['PUT', [], ''], 'GET' => ['GET', [], ''], 'lowercase GET' => ['get', [], ''], 'GET with a body' => ['GET', [