Skip to content

Commit

Permalink
bugfix: normalize rest base paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean O'Brien committed Dec 3, 2024
1 parent 6790e53 commit 45fc4eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 41 deletions.
28 changes: 7 additions & 21 deletions src/Api/Serializer/RestSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function (array $matches) use ($varDefinitions) {
if (!$isModifiedModel
&& $serviceName !== 's3'
) {
$relative = $this->prependPath($relative, $path);
$this->normalizePath($path);
}

// If endpoint has path, remove leading '/' to preserve URI resolution.
Expand Down Expand Up @@ -310,31 +310,17 @@ private function getVarDefinitions($command, $args)
}

/**
* If non-empty path with at least one segment present, compare
* with relative and prepend if starting segments are not duplicated
* Appends trailing slash to non-empty paths with at least one segment
* to ensure proper URI resolution
*
* @param string $relative
* @param string $path
*
* @return string
* @return void
*/
private function prependPath(string $relative, string $path): string
private function normalizePath(string $path): void
{
if (empty($relative) || $relative === '/'
|| empty($path) || $path === '/'
) {
return $relative;
if (!empty($path) && $path !== '/' && substr($path, -1) !== '/') {
$this->endpoint = $this->endpoint->withPath($path . '/');
}

$normalizedPath = rtrim($path, '/');
$normalizedRelative = ltrim($relative, '/');

// Check if $relative starts with $path
if (strpos($normalizedRelative, ltrim($normalizedPath, '/')) === 0) {
// $relative already starts with $path, return $relative
return $relative;
}

return $normalizedPath . '/' . $normalizedRelative;
}
}
20 changes: 0 additions & 20 deletions tests/Api/Serializer/RestJsonSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,26 +220,6 @@ public function testPreparesRequestsWithEndpointWithRequestUriAndPath(): void
);
}

/**
* Simulates a custom endpoint provided with a starting path segment matching the
* modeled `RequestUri` starting path segment
*/
public function testPreparesRequestsWithEndpointWithDuplicateRequestUriAndPath(): void
{
$request = $this->getPathEndpointRequest(
'requestUriOperation',
['PathSegment' => 'bar', 'baz' => 'bar'],
['path' => 'foo']
);
$this->assertSame('POST', $request->getMethod());
$this->assertSame('http://foo.com/foo/bar', (string) $request->getUri());
$this->assertSame('{"baz":"bar"}', (string) $request->getBody());
$this->assertSame(
'application/json',
$request->getHeaderLine('Content-Type')
);
}

public function testPreparesRequestsWithBlobButNoForcedContentType()
{
$request = $this->getRequest('bar', ['baz' => 'bar']);
Expand Down

0 comments on commit 45fc4eb

Please sign in to comment.