Skip to content

Commit 3130f9a

Browse files
committed
feat: multiple methods with different namespaces.
1 parent 6497464 commit 3130f9a

File tree

2 files changed

+102
-59
lines changed

2 files changed

+102
-59
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: 95 additions & 59 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,103 @@ 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'] ?? [];
356-
357-
foreach ($param['schema']['properties'] as $paramName => $value) {
358-
if (!in_array($paramName, $whitelistedParams)) {
359-
unset($param['schema']['properties'][$paramName]);
360-
}
361-
}
336+
return $list;
337+
}
362338

363-
$param['schema']['required'] = $additionalMethod['required'] ?? [];
364-
$params[$key] = $param;
365-
}
366-
}
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+
// Update Response
353+
$responses = $additionalMethod['responses'];
354+
$convertedResponse = [];
355+
356+
foreach ($responses as $desc) {
357+
$code = $desc['code'];
358+
if (isset($desc['model'])) {
359+
if (\is_array($desc['model'])) {
360+
$convertedResponse[$code] = [
361+
'schema' => [
362+
'x-oneOf' => \array_map(fn($model) => [
363+
'$ref' => $model,
364+
], $desc['model']),
365+
],
366+
];
367+
continue;
368+
}
367369

368-
return;
369-
};
370+
$convertedResponse[$code] = [
371+
'schema' => [
372+
'$ref' => $desc['model'],
373+
],
374+
];
375+
}
376+
}
370377

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

373-
// Overwrite description and name if method has one
374-
if (!empty($additionalMethod['name'])) {
375-
$duplicatedMethod['summary'] = $additionalMethod['name'];
380+
// Remove non-whitelisted parameters on body parameters, also set required.
381+
$handleParams = function (&$params) use ($additionalMethod) {
382+
if (isset($additionalMethod['parameters'])) {
383+
foreach ($params as $key => $param) {
384+
if (empty($param['in']) || $param['in'] !== 'body' || empty($param['schema']['properties'])) {
385+
continue;
376386
}
377387

378-
if (!empty($additionalMethod['description'])) {
379-
$duplicatedMethod['description'] = $additionalMethod['description'];
388+
$whitelistedParams = $additionalMethod['parameters'];
389+
390+
foreach ($param['schema']['properties'] as $paramName => $value) {
391+
if (!in_array($paramName, $whitelistedParams)) {
392+
unset($param['schema']['properties'][$paramName]);
393+
}
380394
}
381395

382-
$list[] = $this->parseMethod($methodName, $pathName, $duplicatedMethod);
396+
$param['schema']['required'] = $additionalMethod['required'] ?? [];
397+
$params[$key] = $param;
383398
}
384399
}
400+
};
401+
402+
$handleParams($duplicatedMethod['parameters']);
403+
404+
// Overwrite description and name if method has one
405+
if (!empty($additionalMethod['name'])) {
406+
$duplicatedMethod['summary'] = $additionalMethod['name'];
385407
}
386408

387-
return $list;
409+
if (!empty($additionalMethod['description'])) {
410+
$duplicatedMethod['description'] = $additionalMethod['description'];
411+
}
412+
413+
return $this->parseMethod($methodName, $pathName, $duplicatedMethod);
414+
}
415+
416+
/**
417+
* @param array $method
418+
* @param string $service
419+
* @return string
420+
*/
421+
public function getTargetNamespace(array $method, string $service): string
422+
{
423+
return $method['namespace'] ?? $service;
388424
}
389425

390426
/**

0 commit comments

Comments
 (0)