fix(sdk): emit path parameters as required

A route only matches when every path segment is present, so a path parameter is
always supplied whatever the PHP param declares. OpenAPI also requires
required: true for in: path.

Marking sessionId optional on the three /account/sessions/:sessionId routes
therefore broke every generated client: Go emits a bare identifier for the path
substitution and no longer compiles (undefined: SessionId), and Python makes the
argument Optional and requests /account/sessions/None when it is omitted.
This commit is contained in:
Chirag Aggarwal committed 2026-08-04 16:37:07 +05:30
1 parent 0265957273
commit 764713a6ff
2 files changed
+49

No files matched your search

@@ -832,6 +832,13 @@ class OpenAPI3 extends Format
if ($parameterNode['path']) { // Param is in URL path (directly or through alias)
$node['in'] = 'path';
// A route only matches when every path segment is present, so a
// path parameter is always supplied whatever the PHP param says.
// OpenAPI requires `required: true` here, and generators emit a
// bare identifier for the path substitution — an optional one
// becomes an undefined reference (Go) or interpolates the
// absent value into the URL (Python).
$node['required'] = true;
$methodTemp['parameters'][] = $node;
} elseif (\in_array($method, ['GET', 'DELETE'], true)) { // Param is in query
$node['in'] = 'query';
@@ -321,6 +321,48 @@ final class FormatTest extends TestCase
}
/**
* A route only matches when every path segment is present, so a path
* parameter is always supplied no matter what the PHP param declares.
* Marking one optional produced a Go SDK that does not compile
* (`undefined: SessionId`) and a Python SDK that requested
* `/account/sessions/None`.
*/
public function testOptionalPathParameterIsEmittedAsRequired(): void
{
Method::$processed = [];
Method::$errors = [];
$route = (new Route('GET', '/v1/tests/:sessionId'))
->desc('Get test')
->label('sdk', new Method(
namespace: 'test',
group: null,
name: 'getPathTest',
description: 'Get test.',
auth: [],
responses: [
new SDKResponse(code: 200, model: Response::MODEL_NONE),
],
))
->param('sessionId', 'current', new Text(256), 'Session ID.', true)
->param('filter', '', new Text(256), 'Optional query filter.', true);
$openApi = (new OpenAPI3(new Container(), [], [$route], [new NoneModel()], [], 0, 'console'))->parse();
$parameters = [];
foreach ($openApi['paths']['/tests/{sessionId}']['get']['parameters'] as $parameter) {
$parameters[$parameter['name']] = $parameter;
}
$this->assertSame('path', $parameters['sessionId']['in']);
$this->assertTrue($parameters['sessionId']['required']);
// An optional query parameter is untouched — only the path is forced.
$this->assertSame('query', $parameters['filter']['in']);
$this->assertFalse($parameters['filter']['required']);
}
/**
* The project usage handler writes the text embedding metrics as four
* per-period lists plus four scalar totals. A typed SDK generated from a