Skip to content

Commit c9ebf8d

Browse files
authored
Merge pull request #1128 from appwrite/feat-multiple-namespaces
Feat: Multiple namespaces
2 parents 6497464 + 63832b2 commit c9ebf8d

File tree

2 files changed

+110
-58
lines changed

2 files changed

+110
-58
lines changed

src/Spec/Spec.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ abstract public function getServices();
105105
*/
106106
abstract public function getMethods($service);
107107

108+
/**
109+
* @param array $method
110+
* @param string $service
111+
* @return string
112+
*/
113+
abstract public function getTargetNamespace(array $method, string $service);
114+
108115
/**
109116
* @return string
110117
*/

src/Spec/Swagger2.php

Lines changed: 103 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,19 @@ public function getMethods($service)
302302

303303
foreach ($paths as $pathName => $path) {
304304
foreach ($path as $methodName => $method) {
305-
if (!(isset($method['tags']) && is_array($method['tags']) && in_array($service, $method['tags']))) {
305+
$isCurrentService = isset($method['tags']) && is_array($method['tags']) && in_array($service, $method['tags']);
306+
307+
if (!$isCurrentService) {
308+
if (!empty($method['x-appwrite']['methods'] ?? [])) {
309+
foreach ($method['x-appwrite']['methods'] as $additionalMethod) {
310+
// has multiple namespaced methods!
311+
$targetNamespace = $this->getTargetNamespace($additionalMethod, $service);
312+
313+
if ($targetNamespace === $service) {
314+
$list[] = $this->handleAdditionalMethods($methodName, $pathName, $method, $additionalMethod);
315+
}
316+
}
317+
}
306318
continue;
307319
}
308320

@@ -312,79 +324,112 @@ public function getMethods($service)
312324
}
313325

314326
foreach ($method['x-appwrite']['methods'] as $additionalMethod) {
315-
$duplicatedMethod = $method;
316-
$duplicatedMethod['x-appwrite']['method'] = $additionalMethod['name'];
317-
$duplicatedMethod['x-appwrite']['auth'] = $additionalMethod['auth'] ?? [];
318-
319-
// Update Response
320-
$responses = $additionalMethod['responses'];
321-
$convertedResponse = [];
322-
323-
foreach ($responses as $desc) {
324-
$code = $desc['code'];
325-
if (isset($desc['model'])) {
326-
if (\is_array($desc['model'])) {
327-
$convertedResponse[$code] = [
328-
'schema' => [
329-
'x-oneOf' => \array_map(fn($model) => [
330-
'$ref' => $model,
331-
], $desc['model']),
332-
],
333-
];
334-
continue;
335-
}
327+
$targetNamespace = $this->getTargetNamespace($additionalMethod, $service);
336328

337-
$convertedResponse[$code] = [
338-
'schema' => [
339-
'$ref' => $desc['model'],
340-
],
341-
];
342-
}
329+
if ($targetNamespace === $service) {
330+
$list[] = $this->handleAdditionalMethods($methodName, $pathName, $method, $additionalMethod);
343331
}
332+
}
333+
}
334+
}
344335

345-
$duplicatedMethod['responses'] = $convertedResponse;
346-
347-
// Remove non-whitelisted parameters on body parameters, also set required.
348-
$handleParams = function (&$params) use ($additionalMethod) {
349-
if (isset($additionalMethod['parameters'])) {
350-
foreach ($params as $key => $param) {
351-
if (empty($param['in']) || $param['in'] !== 'body' || empty($param['schema']['properties'])) {
352-
continue;
353-
}
354-
355-
$whitelistedParams = $additionalMethod['parameters'] ?? [];
336+
return $list;
337+
}
356338

357-
foreach ($param['schema']['properties'] as $paramName => $value) {
358-
if (!in_array($paramName, $whitelistedParams)) {
359-
unset($param['schema']['properties'][$paramName]);
360-
}
361-
}
339+
/**
340+
* @param $methodName
341+
* @param $pathName
342+
* @param $method
343+
* @param $additionalMethod
344+
* @return array
345+
*/
346+
private function handleAdditionalMethods($methodName, $pathName, $method, $additionalMethod): array
347+
{
348+
$duplicatedMethod = $method;
349+
$duplicatedMethod['x-appwrite']['method'] = $additionalMethod['name'];
350+
$duplicatedMethod['x-appwrite']['auth'] = $additionalMethod['auth'] ?? [];
351+
352+
if (isset($additionalMethod['deprecated'])) {
353+
$duplicatedMethod['deprecated'] = $additionalMethod['deprecated'];
354+
$duplicatedMethod['x-appwrite']['deprecated'] = $additionalMethod['deprecated'];
355+
} else {
356+
// remove inherited deprecations!
357+
unset($duplicatedMethod['deprecated']);
358+
unset($duplicatedMethod['x-appwrite']['deprecated']);
359+
}
362360

363-
$param['schema']['required'] = $additionalMethod['required'] ?? [];
364-
$params[$key] = $param;
365-
}
366-
}
361+
// Update Response
362+
$responses = $additionalMethod['responses'];
363+
$convertedResponse = [];
364+
365+
foreach ($responses as $desc) {
366+
$code = $desc['code'];
367+
if (isset($desc['model'])) {
368+
if (\is_array($desc['model'])) {
369+
$convertedResponse[$code] = [
370+
'schema' => [
371+
'x-oneOf' => \array_map(fn($model) => [
372+
'$ref' => $model,
373+
], $desc['model']),
374+
],
375+
];
376+
continue;
377+
}
367378

368-
return;
369-
};
379+
$convertedResponse[$code] = [
380+
'schema' => [
381+
'$ref' => $desc['model'],
382+
],
383+
];
384+
}
385+
}
370386

371-
$handleParams($duplicatedMethod['parameters']);
387+
$duplicatedMethod['responses'] = $convertedResponse;
372388

373-
// Overwrite description and name if method has one
374-
if (!empty($additionalMethod['name'])) {
375-
$duplicatedMethod['summary'] = $additionalMethod['name'];
389+
// Remove non-whitelisted parameters on body parameters, also set required.
390+
$handleParams = function (&$params) use ($additionalMethod) {
391+
if (isset($additionalMethod['parameters'])) {
392+
foreach ($params as $key => $param) {
393+
if (empty($param['in']) || $param['in'] !== 'body' || empty($param['schema']['properties'])) {
394+
continue;
376395
}
377396

378-
if (!empty($additionalMethod['description'])) {
379-
$duplicatedMethod['description'] = $additionalMethod['description'];
397+
$whitelistedParams = $additionalMethod['parameters'];
398+
399+
foreach ($param['schema']['properties'] as $paramName => $value) {
400+
if (!in_array($paramName, $whitelistedParams)) {
401+
unset($param['schema']['properties'][$paramName]);
402+
}
380403
}
381404

382-
$list[] = $this->parseMethod($methodName, $pathName, $duplicatedMethod);
405+
$param['schema']['required'] = $additionalMethod['required'] ?? [];
406+
$params[$key] = $param;
383407
}
384408
}
409+
};
410+
411+
$handleParams($duplicatedMethod['parameters']);
412+
413+
// Overwrite description and name if method has one
414+
if (!empty($additionalMethod['name'])) {
415+
$duplicatedMethod['summary'] = $additionalMethod['name'];
385416
}
386417

387-
return $list;
418+
if (!empty($additionalMethod['description'])) {
419+
$duplicatedMethod['description'] = $additionalMethod['description'];
420+
}
421+
422+
return $this->parseMethod($methodName, $pathName, $duplicatedMethod);
423+
}
424+
425+
/**
426+
* @param array $method
427+
* @param string $service
428+
* @return string
429+
*/
430+
public function getTargetNamespace(array $method, string $service): string
431+
{
432+
return $method['namespace'] ?? $service;
388433
}
389434

390435
/**

0 commit comments

Comments
 (0)