Skip to content

Commit

Permalink
* New config option to allow more customisation on the servers added …
Browse files Browse the repository at this point in the history
…to the open api yaml
  • Loading branch information
ProjectZero4 committed Sep 13, 2023
1 parent b32959d commit c55659e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
10 changes: 10 additions & 0 deletions config/scribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
*/
'base_url' => null,

/*
* An array of servers available for the generated docs.
* Overrides base_url when generating the servers list for the openapi.yaml file.
*/
'servers' => [
// [
// 'url' => null,
// 'description' => null,
// ],
],
/*
* Tell Scribe what routes to generate documentation for.
* Each group contains rules defining which routes should be included ('match', 'include' and 'exclude' sections)
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/GenerateDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected function upgradeConfigFileIfNeeded(): void
$upgrader = Upgrader::ofConfigFile("config/{$this->configName}.php", __DIR__ . '/../../config/scribe.php')
->dontTouch(
'routes', 'example_languages', 'database_connections_to_transact', 'strategies', 'laravel.middleware',
'postman.overrides', 'openapi.overrides', 'groups', 'examples.models_source'
'postman.overrides', 'openapi.overrides', 'groups', 'examples.models_source', 'servers',
);
$changes = $upgrader->dryRun();
if (!empty($changes)) {
Expand Down
20 changes: 15 additions & 5 deletions src/Writing/OpenAPISpecWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public function generateSpecContent(array $groupedEndpoints): array
'description' => $this->config->get('description', ''),
'version' => '1.0.0',
],
'servers' => [
[
'url' => rtrim($this->config->get('base_url') ?? config('app.url'), '/'),
],
],
'servers' => $this->generateServers(),
'paths' => $this->generatePathsSpec($groupedEndpoints),
'tags' => array_values(array_map(function (array $group) {
return [
Expand All @@ -58,6 +54,20 @@ public function generateSpecContent(array $groupedEndpoints): array
], $this->generateSecurityPartialSpec());
}

protected function generateServers(): array
{
$servers = $this->config->get('servers', []);
if ($servers) {
return $servers;
}

return [
[
'url' => rtrim($this->config->get('base_url') ?? config('app.url'), '/'),
],
];
}

/**
* @param array[] $groupedEndpoints
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/OpenAPISpecWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class OpenAPISpecWriterTest extends TestCase
'title' => 'My Testy Testes API',
'description' => 'All about testy testes.',
'base_url' => 'http://api.api.dev',
'servers' => [
[
'url' => 'http://api.api.dev',
'description' => 'TheSideProjectAPI (Development)',
],
[
'url' => 'http://api.api.prod',
'description' => 'TheSideProjectAPI (Production)',
],
]
];

/** @test */
Expand All @@ -30,6 +40,9 @@ public function follows_correct_spec_structure()
$endpointData1 = $this->createMockEndpointData();
$endpointData2 = $this->createMockEndpointData();
$groups = [$this->createGroup([$endpointData1, $endpointData2])];
// Remove the servers key, so we can confirm backwards compatibility with "base_url" config option
$ephemeralConfig = $this->config;
unset($this->config['servers']);

$results = $this->generate($groups);

Expand All @@ -40,6 +53,14 @@ public function follows_correct_spec_structure()
$this->assertEquals($this->config['base_url'], $results['servers'][0]['url']);
$this->assertIsArray($results['paths']);
$this->assertGreaterThan(0, count($results['paths']));

// Reset the config, so we can now test the new "servers" config option
$this->config = $ephemeralConfig;
$results = $this->generate($groups);
$this->assertEquals($this->config['servers'][0]['url'], $results['servers'][0]['url']);
$this->assertEquals($this->config['servers'][0]['description'], $results['servers'][0]['description']);
$this->assertEquals($this->config['servers'][1]['url'], $results['servers'][1]['url']);
$this->assertEquals($this->config['servers'][1]['description'], $results['servers'][1]['description']);
}

/** @test */
Expand Down

0 comments on commit c55659e

Please sign in to comment.