mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
fix(specs): use swagger discriminator extension mapping
This commit is contained in:
@@ -263,23 +263,6 @@ abstract class Format
|
||||
return $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string> $types
|
||||
* @return array<Model>
|
||||
*/
|
||||
protected function resolveModels(array $types): array
|
||||
{
|
||||
return \array_map(function (string $type) {
|
||||
foreach ($this->models as $model) {
|
||||
if ($model->getType() === $type) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException("Unresolved model '{$type}'. Ensure the model is registered.");
|
||||
}, $types);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<Model> $models
|
||||
* @return array<string, mixed>|null
|
||||
|
||||
@@ -907,7 +907,15 @@ class OpenAPI3 extends Format
|
||||
return ['$ref' => '#/components/schemas/' . $type];
|
||||
}, $rule['type']),
|
||||
'discriminator' => $this->getDiscriminator(
|
||||
$this->resolveModels($rule['type']),
|
||||
\array_map(function (string $type) {
|
||||
foreach ($this->models as $model) {
|
||||
if ($model->getType() === $type) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException("Unresolved model '{$type}'. Ensure the model is registered.");
|
||||
}, $rule['type']),
|
||||
'#/components/schemas/'
|
||||
),
|
||||
]);
|
||||
@@ -917,7 +925,15 @@ class OpenAPI3 extends Format
|
||||
return ['$ref' => '#/components/schemas/' . $type];
|
||||
}, $rule['type']),
|
||||
'discriminator' => $this->getDiscriminator(
|
||||
$this->resolveModels($rule['type']),
|
||||
\array_map(function (string $type) {
|
||||
foreach ($this->models as $model) {
|
||||
if ($model->getType() === $type) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException("Unresolved model '{$type}'. Ensure the model is registered.");
|
||||
}, $rule['type']),
|
||||
'#/components/schemas/'
|
||||
),
|
||||
]);
|
||||
|
||||
@@ -322,12 +322,17 @@ class Swagger2 extends Format
|
||||
}
|
||||
$temp['responses'][(string)$response->getCode() ?? '500'] = [
|
||||
'description' => $modelDescription,
|
||||
'schema' => \array_filter([
|
||||
'x-oneOf' => \array_map(function ($m) {
|
||||
return ['$ref' => '#/definitions/' . $m->getType()];
|
||||
}, $model),
|
||||
'x-discriminator' => $this->getDiscriminator($model, '#/definitions/'),
|
||||
]),
|
||||
'schema' => (function () use ($model) {
|
||||
$discriminator = $this->getDiscriminator($model, '#/definitions/');
|
||||
|
||||
return \array_filter([
|
||||
'x-oneOf' => \array_map(function ($m) {
|
||||
return ['$ref' => '#/definitions/' . $m->getType()];
|
||||
}, $model),
|
||||
'discriminator' => $discriminator['propertyName'] ?? null,
|
||||
'x-discriminator-mapping' => $discriminator['mapping'] ?? null,
|
||||
]);
|
||||
})(),
|
||||
];
|
||||
} else {
|
||||
// Response definition using one type
|
||||
@@ -882,20 +887,38 @@ class Swagger2 extends Format
|
||||
|
||||
if (\is_array($rule['type'])) {
|
||||
if ($rule['array']) {
|
||||
$resolvedModels = \array_map(function (string $type) {
|
||||
foreach ($this->models as $model) {
|
||||
if ($model->getType() === $type) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException("Unresolved model '{$type}'. Ensure the model is registered.");
|
||||
}, $rule['type']);
|
||||
$discriminator = $this->getDiscriminator($resolvedModels, '#/definitions/');
|
||||
|
||||
$items = \array_filter([
|
||||
'x-anyOf' => \array_map(fn ($type) => ['$ref' => '#/definitions/' . $type], $rule['type']),
|
||||
'x-discriminator' => $this->getDiscriminator(
|
||||
$this->resolveModels($rule['type']),
|
||||
'#/definitions/'
|
||||
),
|
||||
'discriminator' => $discriminator['propertyName'] ?? null,
|
||||
'x-discriminator-mapping' => $discriminator['mapping'] ?? null,
|
||||
]);
|
||||
} else {
|
||||
$resolvedModels = \array_map(function (string $type) {
|
||||
foreach ($this->models as $model) {
|
||||
if ($model->getType() === $type) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \RuntimeException("Unresolved model '{$type}'. Ensure the model is registered.");
|
||||
}, $rule['type']);
|
||||
$discriminator = $this->getDiscriminator($resolvedModels, '#/definitions/');
|
||||
|
||||
$items = \array_filter([
|
||||
'x-oneOf' => \array_map(fn ($type) => ['$ref' => '#/definitions/' . $type], $rule['type']),
|
||||
'x-discriminator' => $this->getDiscriminator(
|
||||
$this->resolveModels($rule['type']),
|
||||
'#/definitions/'
|
||||
),
|
||||
'discriminator' => $discriminator['propertyName'] ?? null,
|
||||
'x-discriminator-mapping' => $discriminator['mapping'] ?? null,
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user