diff --git a/src/Spec/Swagger2.php b/src/Spec/Swagger2.php index 384aeb8c18..c5ffcf4627 100644 --- a/src/Spec/Swagger2.php +++ b/src/Spec/Swagger2.php @@ -662,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 90ca9d2412..404da7338c 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->getAllEnums(); + $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(['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')); + $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..88e1e4a41a 100644 --- a/tests/resources/spec.json +++ b/tests/resources/spec.json @@ -2258,6 +2258,23 @@ ] } }, + "x-sdk-enums": [ + { + "name": "MockPlan", + "enum": [ + "tier-0", + "tier-1", + "tier-2", + "ent-1" + ], + "keys": [ + "Free", + "Pro", + "Scale", + "Enterprise" + ] + } + ], "externalDocs": { "description": "Full API docs, specs and tutorials", "url": "https:\/\/appwrite.io\/docs"