Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk-php): refuse a non-JSON success body in a typed call
Yudhi Armyndharisb204e2a8f52e
diff --git a/sdk/php/src/Client.php b/sdk/php/src/Client.phpindex c26d562da..eeaa9a870 100644--- a/sdk/php/src/Client.php+++ b/sdk/php/src/Client.php@@ -180,6 +180,6 @@ class Client*/public function request(string $method, string $path, array $query = [], mixed $body = null): mixed{- return $this->http->request($method, $path, $query, $body);+ return $this->http->request($method, $path, $query, $body, null, true);}}diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.phpindex 61bf8b037..a2a4af023 100644--- a/sdk/php/src/Http/HttpExecutor.php+++ b/sdk/php/src/Http/HttpExecutor.php@@ -157,13 +157,24 @@ class HttpExecutor* @param array<string,mixed> $query Query parameters (null values skipped).* @param mixed|null $body JSON-serializable request body.*+ * @param bool $allowText Return a 2xx body that is not JSON as its text. Off for the typed+ * resources, whose callers expect an array: they get an exception that+ * names the request, not a TypeError from the return type.+ ** @return mixed Decoded JSON, or null for empty/204 responses.** @throws OpenWAApiException On any non-2xx response (typed subclass).* @throws OpenWATimeoutException On timeout.+ * @throws OpenWAException On a 2xx body that is not JSON, unless $allowText.*/- public function request(string $method, string $path, array $query = [], $body = null, ?string $idempotencyKey = null)- {+ public function request(+ string $method,+ string $path,+ array $query = [],+ $body = null,+ ?string $idempotencyKey = null,+ bool $allowText = false+ ) {$response = $this->send($method, $path, $query, $body, $idempotencyKey);$text = (string) $response->getBody();@@ -172,7 +183,13 @@ class HttpExecutor}$decoded = json_decode($text, true);- return $decoded === null && json_last_error() !== JSON_ERROR_NONE ? $text : $decoded;+ if ($decoded === null && json_last_error() !== JSON_ERROR_NONE) {+ if (!$allowText) {+ throw new OpenWAException("OpenWA: non-JSON response for {$method} {$path}");+ }+ return $text;+ }+ return $decoded;}/**diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.phpindex 5c78261dd..16ec0eac3 100644--- a/sdk/php/tests/ClientTest.php+++ b/sdk/php/tests/ClientTest.php@@ -587,6 +587,27 @@ class ClientTest extends TestCase$this->assertNull($client->request('GET', '/api/anything'));}+ public function testATypedResourceRefusesASuccessBodyThatIsNotJson(): void+ {+ $backend = new MockBackend();+ $client = $backend->makeClient();+ // A baseUrl that points at a page server answers every route 200 with HTML.+ $backend->onRaw(200, '<html>sign in</html>', ['Content-Type' => 'text/html']);+ $backend->onRaw(200, '<html>sign in</html>', ['Content-Type' => 'text/html']);+ $calls = [+ fn () => $client->sessions->get('s'),+ fn () => $client->auth(),+ ];+ foreach ($calls as $call) {+ try {+ $call();+ $this->fail('Expected an OpenWAException for a body that is not JSON');+ } catch (\OpenWA\Exceptions\OpenWAException $e) {+ $this->assertStringContainsString('non-JSON response for', $e->getMessage());+ }+ }+ }+public function testAnErrorBodyThatIsNotJsonIsKeptAsText(): void{$backend = new MockBackend();