Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Spec/Swagger2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
17 changes: 17 additions & 0 deletions tests/resources/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading