Skip to content

Commit 84f9bd5

Browse files
authored
bugfix: normalize rest base paths (#3042)
1 parent 11bb270 commit 84f9bd5

File tree

2 files changed

+9
-42
lines changed

2 files changed

+9
-42
lines changed

src/Api/Serializer/RestSerializer.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,11 @@ function (array $matches) use ($varDefinitions) {
243243
}
244244
}
245245

246-
if (!$isModifiedModel
246+
if ((!empty($relative) && $relative !== '/')
247+
&& !$isModifiedModel
247248
&& $serviceName !== 's3'
248249
) {
249-
$relative = $this->prependPath($relative, $path);
250+
$this->normalizePath($path);
250251
}
251252

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

312313
/**
313-
* If non-empty path with at least one segment present, compare
314-
* with relative and prepend if starting segments are not duplicated
314+
* Appends trailing slash to non-empty paths with at least one segment
315+
* to ensure proper URI resolution
315316
*
316-
* @param string $relative
317317
* @param string $path
318318
*
319-
* @return string
319+
* @return void
320320
*/
321-
private function prependPath(string $relative, string $path): string
321+
private function normalizePath(string $path): void
322322
{
323-
if (empty($relative) || $relative === '/'
324-
|| empty($path) || $path === '/'
325-
) {
326-
return $relative;
323+
if (!empty($path) && $path !== '/' && substr($path, -1) !== '/') {
324+
$this->endpoint = $this->endpoint->withPath($path . '/');
327325
}
328-
329-
$normalizedPath = rtrim($path, '/');
330-
$normalizedRelative = ltrim($relative, '/');
331-
332-
// Check if $relative starts with $path
333-
if (strpos($normalizedRelative, ltrim($normalizedPath, '/')) === 0) {
334-
// $relative already starts with $path, return $relative
335-
return $relative;
336-
}
337-
338-
return $normalizedPath . '/' . $normalizedRelative;
339326
}
340327
}

tests/Api/Serializer/RestJsonSerializerTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -220,26 +220,6 @@ public function testPreparesRequestsWithEndpointWithRequestUriAndPath(): void
220220
);
221221
}
222222

223-
/**
224-
* Simulates a custom endpoint provided with a starting path segment matching the
225-
* modeled `RequestUri` starting path segment
226-
*/
227-
public function testPreparesRequestsWithEndpointWithDuplicateRequestUriAndPath(): void
228-
{
229-
$request = $this->getPathEndpointRequest(
230-
'requestUriOperation',
231-
['PathSegment' => 'bar', 'baz' => 'bar'],
232-
['path' => 'foo']
233-
);
234-
$this->assertSame('POST', $request->getMethod());
235-
$this->assertSame('http://foo.com/foo/bar', (string) $request->getUri());
236-
$this->assertSame('{"baz":"bar"}', (string) $request->getBody());
237-
$this->assertSame(
238-
'application/json',
239-
$request->getHeaderLine('Content-Type')
240-
);
241-
}
242-
243223
public function testPreparesRequestsWithBlobButNoForcedContentType()
244224
{
245225
$request = $this->getRequest('bar', ['baz' => 'bar']);

0 commit comments

Comments
 (0)