From 764713a6ff6a45696ecbc61443dfa71ab1354817 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 4 Aug 2026 16:37:07 +0530 Subject: [PATCH] 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. --- .../SDK/Specification/Format/OpenAPI3.php | 7 ++++ tests/unit/SDK/Specification/FormatTest.php | 42 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php index 04f18cbab6..2d3c59786b 100644 --- a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php @@ -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'; diff --git a/tests/unit/SDK/Specification/FormatTest.php b/tests/unit/SDK/Specification/FormatTest.php index 1412536ecb..8ca29b6b22 100644 --- a/tests/unit/SDK/Specification/FormatTest.php +++ b/tests/unit/SDK/Specification/FormatTest.php @@ -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