diff --git a/.changes/nextrelease/changelog_endpoint_path.json b/.changes/nextrelease/changelog_endpoint_path.json new file mode 100644 index 0000000000..1f8c71b9e4 --- /dev/null +++ b/.changes/nextrelease/changelog_endpoint_path.json @@ -0,0 +1,7 @@ +[ + { + "type": "bugfix", + "category": "Api", + "description": "Preserve path on custom endpoints" + } +] diff --git a/src/Api/Serializer/RestSerializer.php b/src/Api/Serializer/RestSerializer.php index 3dbb26673c..96e66cd8b1 100644 --- a/src/Api/Serializer/RestSerializer.php +++ b/src/Api/Serializer/RestSerializer.php @@ -212,6 +212,12 @@ function (array $matches) use ($varspecs) { $relative .= strpos($relative, '?') ? "&{$append}" : "?$append"; } + // If endpoint has path, remove leading '/' to preserve URI resolution. + $path = $this->endpoint->getPath(); + if ($path && $relative[0] === '/') { + $relative = substr($relative, 1); + } + // Expand path place holders using Amazon's slightly different URI // template syntax. return UriResolver::resolve($this->endpoint, new Uri($relative)); diff --git a/tests/Api/Serializer/RestJsonSerializerTest.php b/tests/Api/Serializer/RestJsonSerializerTest.php index 49b5dd669e..a118af2107 100644 --- a/tests/Api/Serializer/RestJsonSerializerTest.php +++ b/tests/Api/Serializer/RestJsonSerializerTest.php @@ -86,6 +86,14 @@ private function getRequest($commandName, $input) return $j($command); } + private function getPathEndpointRequest($commandName, $input) + { + $service = $this->getTestService(); + $command = new Command($commandName, $input); + $j = new RestJsonSerializer($service, 'http://foo.com/bar'); + return $j($command); + } + public function testPreparesRequestsWithContentType() { $request = $this->getRequest('foo', ['baz' => 'bar']); @@ -98,6 +106,18 @@ public function testPreparesRequestsWithContentType() ); } + public function testPreparesRequestsWithEndpointWithPath() + { + $request = $this->getPathEndpointRequest('foo', ['baz' => 'bar']); + $this->assertEquals('POST', $request->getMethod()); + $this->assertEquals('http://foo.com/bar', (string) $request->getUri()); + $this->assertEquals('{"baz":"bar"}', (string) $request->getBody()); + $this->assertEquals( + 'application/json', + $request->getHeaderLine('Content-Type') + ); + } + public function testPreparesRequestsWithBlobButNoForcedContentType() { $request = $this->getRequest('bar', ['baz' => 'bar']);