Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: normalize rest base paths #3042

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions src/Api/Serializer/RestSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,11 @@ function (array $matches) use ($varDefinitions) {
}
}

if (!$isModifiedModel
if ((!empty($relative) && $relative !== '/')
&& !$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 +311,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
Loading