Skip to content

Commit

Permalink
fix: maintain forwards compatibility with REST server streaming (goog…
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored and alicejli committed Feb 4, 2022
1 parent 59a9817 commit 797ab37
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.11.3
1.11.4
10 changes: 10 additions & 0 deletions src/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/Transport/RestTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class RestTransport implements TransportInterface
{
use ValidationTrait;
use ServiceAddressTrait;
use HttpUnaryTransportTrait;
use HttpUnaryTransportTrait {
startServerStreamingCall as protected unsupportedServerStreamingCall;
}

private $requestBuilder;

Expand Down Expand Up @@ -145,6 +147,7 @@ function (\Exception $ex) {

/**
* {@inheritdoc}
* @throws BadMethodCallException for forwards compatibility with older GAPIC clients
*/
public function startServerStreamingCall(Call $call, array $options)
{
Expand All @@ -153,6 +156,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())) {
$this->unsupportedServerStreamingCall($call, $options);
}

$headers = self::buildCommonHeaders($options);
$callOptions = $this->getCallOptions($options);
$request = $this->requestBuilder->build(
Expand Down
19 changes: 19 additions & 0 deletions tests/Tests/Unit/Transport/RestTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ private function getTransport(callable $httpHandler = null, $apiEndpoint = 'http
->getMock();
$requestBuilder->method('build')
->willReturn($request);
$requestBuilder->method('pathExists')
->willReturn(true);

return new RestTransport(
$requestBuilder,
Expand Down Expand Up @@ -136,6 +138,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
*/
Expand Down

0 comments on commit 797ab37

Please sign in to comment.