From 4c36e8b7ff3b9547b6d2db711ed10c125f27aae9 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 26 Jan 2022 07:36:39 -0600 Subject: [PATCH 1/4] fix: maintain forwards compatibility with REST server streaming --- src/RequestBuilder.php | 10 ++++++++++ src/Transport/RestTransport.php | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/src/RequestBuilder.php b/src/RequestBuilder.php index 808ed033f..dcfdfb7dc 100644 --- a/src/RequestBuilder.php +++ b/src/RequestBuilder.php @@ -65,6 +65,16 @@ public function __construct($baseUri, $restConfigPath) $this->restConfig = require($restConfigPath); } + /** + * @param string $path + * @return bool + */ + public function pathExists($path) + { + list($interface, $method) = explode('/', $path); + return isset($this->restConfig['interfaces'][$interface][$method]); + } + /** * @param string $path * @param Message $message diff --git a/src/Transport/RestTransport.php b/src/Transport/RestTransport.php index 4ec5fba56..33c8bd618 100644 --- a/src/Transport/RestTransport.php +++ b/src/Transport/RestTransport.php @@ -42,6 +42,7 @@ use Google\Protobuf\Internal\Message; use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\ResponseInterface; +use BadMethodCallException; /** * A REST based transport implementation. @@ -145,6 +146,7 @@ function (\Exception $ex) { /** * {@inheritdoc} + * @throws BadMethodCallException for forwards compatibility with older GAPIC clients */ public function startServerStreamingCall(Call $call, array $options) { @@ -153,6 +155,12 @@ public function startServerStreamingCall(Call $call, array $options) throw new \InvalidArgumentException('A message is required for ServerStreaming calls.'); } + // Maintain forwards compatibility with older GAPIC clients not configured for REST server streaming + // @see https://github.com/googleapis/gax-php/issues/370 + if (!$this->requestBuilder->pathExists($call->getMethod())) { + throw new BadMethodCallException('Streaming calls are not supported while using the REST transport'); + } + $headers = self::buildCommonHeaders($options); $callOptions = $this->getCallOptions($options); $request = $this->requestBuilder->build( From 2a6754322a35115a0614ef60cee15bf483a52078 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 26 Jan 2022 12:28:15 -0600 Subject: [PATCH 2/4] fix tests --- tests/Tests/Unit/Transport/RestTransportTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Tests/Unit/Transport/RestTransportTest.php b/tests/Tests/Unit/Transport/RestTransportTest.php index 36238e931..0ede49fbf 100644 --- a/tests/Tests/Unit/Transport/RestTransportTest.php +++ b/tests/Tests/Unit/Transport/RestTransportTest.php @@ -73,6 +73,8 @@ private function getTransport(callable $httpHandler = null, $apiEndpoint = 'http ->getMock(); $requestBuilder->method('build') ->willReturn($request); + $requestBuilder->method('pathExists') + ->willReturn(true); return new RestTransport( $requestBuilder, From f25e1e703fc74cc0fb85ddef8070a8f8fb00e5f9 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 26 Jan 2022 12:51:27 -0600 Subject: [PATCH 3/4] use downstream trait error --- src/Transport/RestTransport.php | 7 ++++--- .../Tests/Unit/Transport/RestTransportTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Transport/RestTransport.php b/src/Transport/RestTransport.php index 33c8bd618..24582c849 100644 --- a/src/Transport/RestTransport.php +++ b/src/Transport/RestTransport.php @@ -42,7 +42,6 @@ use Google\Protobuf\Internal\Message; use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\ResponseInterface; -use BadMethodCallException; /** * A REST based transport implementation. @@ -51,7 +50,9 @@ class RestTransport implements TransportInterface { use ValidationTrait; use ServiceAddressTrait; - use HttpUnaryTransportTrait; + use HttpUnaryTransportTrait { + startServerStreamingCall as protected unsupportedServerStreamingCall; + } private $requestBuilder; @@ -158,7 +159,7 @@ public function startServerStreamingCall(Call $call, array $options) // Maintain forwards compatibility with older GAPIC clients not configured for REST server streaming // @see https://github.com/googleapis/gax-php/issues/370 if (!$this->requestBuilder->pathExists($call->getMethod())) { - throw new BadMethodCallException('Streaming calls are not supported while using the REST transport'); + $this->unsupportedServerStreamingCall($call, $options); } $headers = self::buildCommonHeaders($options); diff --git a/tests/Tests/Unit/Transport/RestTransportTest.php b/tests/Tests/Unit/Transport/RestTransportTest.php index 0ede49fbf..5f97aead5 100644 --- a/tests/Tests/Unit/Transport/RestTransportTest.php +++ b/tests/Tests/Unit/Transport/RestTransportTest.php @@ -139,6 +139,23 @@ public function testStartUnaryCallThrowsException() ->wait(); } + /** + * @expectedException \BadMethodCallException + */ + public function testServerStreamingCallThrowsBadMethodCallException() + { + $request = new Request('POST', 'http://www.example.com'); + $requestBuilder = $this->getMockBuilder(RequestBuilder::class) + ->disableOriginalConstructor() + ->getMock(); + $requestBuilder->method('pathExists') + ->willReturn(false); + + $transport = new RestTransport($requestBuilder, HttpHandlerFactory::build()); + + $transport->startServerStreamingCall($this->call, []); + } + /** * @expectedException \Google\ApiCore\ApiException */ From f964939ad406b5c966375b604d3703ebf1b98119 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 26 Jan 2022 12:52:02 -0600 Subject: [PATCH 4/4] increment version --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 0a5af26df..3d0e62313 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.11.3 +1.11.4