Skip to content

Commit

Permalink
Support to define servers and info for swagger. (#7149)
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo authored Nov 11, 2024
1 parent 13dafee commit 10ac9f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions publish/swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@
'processors' => [
// users can append their own processors here
],
'server' => [
'http' => [
'servers' => [
[
'url' => 'http://127.0.0.1:9501',
'description' => 'Test Server',
],
],
'info' => [
'title' => 'Sample API',
'description' => 'This is a sample API using OpenAPI 3.0 specification',
'version' => '1.0.0',
],
],
],
];
10 changes: 10 additions & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function generate(): void
throw new InvalidArgumentException('The processors of swagger must be array.');
}

$servers = (array) $this->config->get('swagger.server', []);

$generator = new \OpenApi\Generator();
$openapi = $generator->setAliases(\OpenApi\Generator::DEFAULT_ALIASES)
->setNamespaces(\OpenApi\Generator::DEFAULT_NAMESPACES)
Expand Down Expand Up @@ -70,6 +72,14 @@ public function generate(): void
$result[$serverName] = $jsonArray;
}

if ($svs = $servers[$serverName]['servers'] ?? null) {
$result[$serverName]['servers'] = $svs;
}

if ($info = $servers[$serverName]['info'] ?? null) {
$result[$serverName]['info'] = $info;
}

$result[$serverName]['paths'][$key] = $path;
}

Expand Down

0 comments on commit 10ac9f1

Please sign in to comment.