From 46d81469662d8698f184375978e544fd02f7e20d Mon Sep 17 00:00:00 2001 From: Darshan Date: Mon, 29 Dec 2025 15:48:35 +0530 Subject: [PATCH 1/4] add: support for standalone enums to be declared and created. --- src/Spec/Swagger2.php | 14 ++++++++++++++ tests/Base.php | 37 +++++++++++++++++++++++++++++++++++++ tests/resources/spec.json | 11 +++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/Spec/Swagger2.php b/src/Spec/Swagger2.php index 384aeb8c18..2e47e1b138 100644 --- a/src/Spec/Swagger2.php +++ b/src/Spec/Swagger2.php @@ -620,6 +620,20 @@ public function getRequestEnums(): array public function getResponseEnums(): array { $list = []; + + // check for standalone enums list + $standaloneEnums = $this->getAttribute('x-sdk-enums', []); + foreach ($standaloneEnums as $enumConfig) { + $enumName = $enumConfig['name'] ?? null; + if ($enumName && !isset($list[$enumName])) { + $list[$enumName] = [ + 'name' => $enumName, + 'enum' => $enumConfig['values'] ?? [], + 'keys' => $enumConfig['keys'] ?? [], + ]; + } + } + $definitions = $this->getDefinitions(); foreach ($definitions as $modelName => $model) { diff --git a/tests/Base.php b/tests/Base.php index 90ca9d2412..67cac882b4 100644 --- a/tests/Base.php +++ b/tests/Base.php @@ -343,4 +343,41 @@ public function getLanguage(): Language { return new $this->class(); } + + /** + * Test that x-sdk-enums are properly parsed and included in response enums + */ + public function testSDKEnums(): void + { + $spec = file_get_contents(realpath(__DIR__ . '/resources/spec.json')); + + if (empty($spec)) { + throw new \Exception('Failed to parse spec.'); + } + + $swagger = new Swagger2($spec); + $enums = $swagger->getResponseEnums(); + $enumNames = array_column($enums, 'name'); + + // Find MockPlan enum from x-sdk-enums + $mockPlanEnum = null; + foreach ($enums as $enum) { + if ($enum['name'] === 'MockPlan') { + $mockPlanEnum = $enum; + break; + } + } + + // x-sdk-enums are included + $this->assertNotNull($mockPlanEnum, 'x-sdk-enums should be included in response enums'); + $this->assertArrayHasKey('enum', $mockPlanEnum); + $this->assertEquals(['free', 'pro', 'enterprise'], $mockPlanEnum['enum']); + + // x-sdk-enums don't duplicate + $mockPlanCount = count(array_filter($enums, fn($e) => $e['name'] === 'MockPlan')); + $this->assertEquals(1, $mockPlanCount, 'x-sdk-enums should not duplicate'); + + // regular property-level enums still work + $this->assertGreaterThan(1, count($enumNames), 'Both x-sdk-enums and property-level enums should be included'); + } } diff --git a/tests/resources/spec.json b/tests/resources/spec.json index db1f4fdad5..eeb64b8042 100644 --- a/tests/resources/spec.json +++ b/tests/resources/spec.json @@ -2258,6 +2258,17 @@ ] } }, + "x-sdk-enums": [ + { + "name": "MockPlan", + "values": [ + "free", + "pro", + "scale", + "enterprise" + ] + } + ], "externalDocs": { "description": "Full API docs, specs and tutorials", "url": "https:\/\/appwrite.io\/docs" From 31f1bc1244da000b72888d3e77f173554f8810df Mon Sep 17 00:00:00 2001 From: Darshan Date: Mon, 29 Dec 2025 15:50:49 +0530 Subject: [PATCH 2/4] fix: test. --- tests/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Base.php b/tests/Base.php index 67cac882b4..44fccadd76 100644 --- a/tests/Base.php +++ b/tests/Base.php @@ -371,7 +371,7 @@ public function testSDKEnums(): void // x-sdk-enums are included $this->assertNotNull($mockPlanEnum, 'x-sdk-enums should be included in response enums'); $this->assertArrayHasKey('enum', $mockPlanEnum); - $this->assertEquals(['free', 'pro', 'enterprise'], $mockPlanEnum['enum']); + $this->assertEquals(['free', 'pro', 'scale', 'enterprise'], $mockPlanEnum['enum']); // x-sdk-enums don't duplicate $mockPlanCount = count(array_filter($enums, fn($e) => $e['name'] === 'MockPlan')); From 7504b6f43fa7c834a93c35a8afd8160f46d553ae Mon Sep 17 00:00:00 2001 From: Darshan Date: Mon, 29 Dec 2025 16:04:34 +0530 Subject: [PATCH 3/4] update: format. --- src/Spec/Swagger2.php | 2 +- tests/Base.php | 2 +- tests/resources/spec.json | 16 +++++++++++----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Spec/Swagger2.php b/src/Spec/Swagger2.php index 2e47e1b138..c3a250a267 100644 --- a/src/Spec/Swagger2.php +++ b/src/Spec/Swagger2.php @@ -628,7 +628,7 @@ public function getResponseEnums(): array if ($enumName && !isset($list[$enumName])) { $list[$enumName] = [ 'name' => $enumName, - 'enum' => $enumConfig['values'] ?? [], + 'enum' => $enumConfig['enum'] ?? [], 'keys' => $enumConfig['keys'] ?? [], ]; } diff --git a/tests/Base.php b/tests/Base.php index 44fccadd76..1e281e6935 100644 --- a/tests/Base.php +++ b/tests/Base.php @@ -371,7 +371,7 @@ public function testSDKEnums(): void // x-sdk-enums are included $this->assertNotNull($mockPlanEnum, 'x-sdk-enums should be included in response enums'); $this->assertArrayHasKey('enum', $mockPlanEnum); - $this->assertEquals(['free', 'pro', 'scale', 'enterprise'], $mockPlanEnum['enum']); + $this->assertEquals(['tier-0', 'tier-1', 'tier-2', 'ent-1'], $mockPlanEnum['enum']); // x-sdk-enums don't duplicate $mockPlanCount = count(array_filter($enums, fn($e) => $e['name'] === 'MockPlan')); diff --git a/tests/resources/spec.json b/tests/resources/spec.json index eeb64b8042..88e1e4a41a 100644 --- a/tests/resources/spec.json +++ b/tests/resources/spec.json @@ -2261,11 +2261,17 @@ "x-sdk-enums": [ { "name": "MockPlan", - "values": [ - "free", - "pro", - "scale", - "enterprise" + "enum": [ + "tier-0", + "tier-1", + "tier-2", + "ent-1" + ], + "keys": [ + "Free", + "Pro", + "Scale", + "Enterprise" ] } ], From 5e3bdc69f09eeaf769da35a643fdeb545552c2f7 Mon Sep 17 00:00:00 2001 From: Darshan Date: Mon, 29 Dec 2025 16:19:09 +0530 Subject: [PATCH 4/4] simplify. --- src/Spec/Swagger2.php | 28 ++++++++++++++-------------- tests/Base.php | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Spec/Swagger2.php b/src/Spec/Swagger2.php index c3a250a267..c5ffcf4627 100644 --- a/src/Spec/Swagger2.php +++ b/src/Spec/Swagger2.php @@ -620,20 +620,6 @@ public function getRequestEnums(): array public function getResponseEnums(): array { $list = []; - - // check for standalone enums list - $standaloneEnums = $this->getAttribute('x-sdk-enums', []); - foreach ($standaloneEnums as $enumConfig) { - $enumName = $enumConfig['name'] ?? null; - if ($enumName && !isset($list[$enumName])) { - $list[$enumName] = [ - 'name' => $enumName, - 'enum' => $enumConfig['enum'] ?? [], - 'keys' => $enumConfig['keys'] ?? [], - ]; - } - } - $definitions = $this->getDefinitions(); foreach ($definitions as $modelName => $model) { @@ -676,6 +662,20 @@ public function getResponseEnums(): array public function getAllEnums(): array { $list = []; + + // check for standalone enums list + $standaloneEnums = $this->getAttribute('x-sdk-enums', []); + foreach ($standaloneEnums as $enumConfig) { + $enumName = $enumConfig['name'] ?? null; + if ($enumName && !isset($list[$enumName])) { + $list[$enumName] = [ + 'name' => $enumName, + 'enum' => $enumConfig['enum'] ?? [], + 'keys' => $enumConfig['keys'] ?? [], + ]; + } + } + foreach ($this->getRequestEnums() as $enum) { $list[$enum['name']] = $enum; } diff --git a/tests/Base.php b/tests/Base.php index 1e281e6935..404da7338c 100644 --- a/tests/Base.php +++ b/tests/Base.php @@ -356,7 +356,7 @@ public function testSDKEnums(): void } $swagger = new Swagger2($spec); - $enums = $swagger->getResponseEnums(); + $enums = $swagger->getAllEnums(); $enumNames = array_column($enums, 'name'); // Find MockPlan enum from x-sdk-enums