Skip to content

Commit

Permalink
fix: replace hardcoded references to openapi.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathanaël Houn committed Mar 4, 2024
1 parent 3c87224 commit 8030aad
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion routes/laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
})->name('scribe.postman');

Route::get("$prefix.openapi", function () {
return response()->file(Storage::disk('local')->path('scribe/openapi.yaml'));
return response()->file(Storage::disk('local')->path('scribe/openapi.' . config('scribe.openapi.format')));
})->name('scribe.openapi');
});
2 changes: 1 addition & 1 deletion routes/lumen.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
return new JsonResponse(Storage::disk('local')->get('scribe/collection.json'), json: true);
})->name('scribe.postman');
$router->get("$prefix.openapi", function () {
return response()->file(Storage::disk('local')->path('scribe/openapi.yaml'));
return response()->file(Storage::disk('local')->path('scribe/openapi.' . config('scribe.openapi.format')));
})->name('scribe.openapi');
});
2 changes: 1 addition & 1 deletion src/Writing/ExternalHtmlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getMetadata(): array
$postmanCollectionUrl = "{$this->assetPathPrefix}collection.json";
}
if ($this->config->get('openapi.enabled', false)) {
$openApiSpecUrl = "{$this->assetPathPrefix}openapi.yaml";
$openApiSpecUrl = "{$this->assetPathPrefix}openapi." . config('scribe.openapi.format');
}
return [
'title' => $this->config->get('title') ?: config('app.name', '') . ' Documentation',
Expand Down
4 changes: 2 additions & 2 deletions src/Writing/HtmlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public function getMetadata(): array
$postmanCollectionUrl = "{$this->assetPathPrefix}collection.json";
}
if ($this->config->get('openapi.enabled', false)) {
$links[] = "<a href=\"{$this->assetPathPrefix}openapi.yaml\">".u::trans("scribe::links.openapi")."</a>";
$openApiSpecUrl = "{$this->assetPathPrefix}openapi.yaml";
$links[] = "<a href=\"{$this->assetPathPrefix}openapi.".config('scribe.openapi.format')."\">".u::trans("scribe::links.openapi")."</a>";
$openApiSpecUrl = "{$this->assetPathPrefix}openapi.".config('scribe.openapi.format');
}

$auth = $this->config->get('auth');
Expand Down
4 changes: 2 additions & 2 deletions src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ protected function performFinalTasksForLaravelType(): void
$contents = preg_replace('#href="\.\./docs/css/(.+?)"#', 'href="{{ asset("' . $this->laravelAssetsPath . '/css/$1") }}"', $contents);
$contents = preg_replace('#src="\.\./docs/(js|images)/(.+?)"#', 'src="{{ asset("' . $this->laravelAssetsPath . '/$1/$2") }}"', $contents);
$contents = str_replace('href="../docs/collection.json"', 'href="{{ route("' . $this->paths->outputPath('postman', '.') . '") }}"', $contents);
$contents = str_replace('href="../docs/openapi.yaml"', 'href="{{ route("' . $this->paths->outputPath('openapi', '.') . '") }}"', $contents);
$contents = str_replace('url="../docs/openapi.yaml"', 'url="{{ route("' . $this->paths->outputPath('openapi', '.') . '") }}"', $contents);
$contents = str_replace('href="../docs/openapi.' . config('scribe.openapi.format') . '"', 'href="{{ route("' . $this->paths->outputPath('openapi', '.') . '") }}"', $contents);
$contents = str_replace('url="../docs/openapi.' . config('scribe.openapi.format') . '"', 'url="{{ route("' . $this->paths->outputPath('openapi', '.') . '") }}"', $contents);

file_put_contents("$this->laravelTypeOutputPath/index.blade.php", $contents);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/GenerateDocumentation/BehavioursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function calls_afterGenerating_hook()
'html' => realpath('public/docs/index.html'),
'blade' => null,
'postman' => realpath('public/docs/collection.json') ?: null,
'openapi' => realpath('public/docs/openapi.yaml') ?: null,
'openapi' => realpath('public/docs/openapi.'.config('scribe.openapi.format')) ?: null,
'assets' => [
'js' => realpath('public/docs/js'),
'css' => realpath('public/docs/css'),
Expand Down
39 changes: 36 additions & 3 deletions tests/GenerateDocumentation/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function generated_postman_collection_file_is_correct()
}

/** @test */
public function generated_openapi_spec_file_is_correct()
public function generated_openapi_spec_file_is_correct_yaml()
{
RouteFacade::post('/api/withBodyParametersAsArray', [TestController::class, 'withBodyParametersAsArray']);
RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
Expand Down Expand Up @@ -232,6 +232,37 @@ public function generated_openapi_spec_file_is_correct()
$this->assertEquals($fixtureSpec, $generatedSpec);
}

/** @test */
public function generated_openapi_spec_file_is_correct_json()
{
RouteFacade::post('/api/withBodyParametersAsArray', [TestController::class, 'withBodyParametersAsArray']);
RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
RouteFacade::get('/api/withResponseTag', [TestController::class, 'withResponseTag']);
RouteFacade::get('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
RouteFacade::get('/api/withAuthTag', [TestController::class, 'withAuthenticatedTag']);
RouteFacade::get('/api/echoesUrlParameters/{param}/{param2}/{param3?}/{param4?}', [TestController::class, 'echoesUrlParameters']);

$this->setConfig([
'openapi.enabled' => true,
'openapi.format' => 'json',
'openapi.overrides' => [
'info.version' => '3.9.9',
],
'routes.0.apply.headers' => [
'Custom-Header' => 'NotSoCustom',
],
]);

$this->generateAndExpectConsoleOutput(
"Wrote HTML docs and assets to: public/docs/",
"Wrote OpenAPI specification to: public/docs/openapi.json"
);

$generatedSpec = json_decode(file_get_contents($this->openapiOutputPath(extension: 'json')), associative: true);
$fixtureSpec = Yaml::parseFile(__DIR__ . '/../Fixtures/openapi.yaml');
$this->assertEquals($fixtureSpec, $generatedSpec);
}

/** @test */
public function can_append_custom_http_headers()
{
Expand Down Expand Up @@ -623,10 +654,12 @@ protected function postmanOutputPath(bool $laravelType = false): string
? Storage::disk('local')->path('scribe/collection.json') : 'public/docs/collection.json';
}

protected function openapiOutputPath(bool $laravelType = false): string
protected function openapiOutputPath(bool $laravelType = false, string $extension = 'yaml'): string
{
$filename = "openapi.{$extension}";

return $laravelType
? Storage::disk('local')->path('scribe/openapi.yaml') : 'public/docs/openapi.yaml';
? Storage::disk('local')->path("scribe/$filename") : ("public/docs/$filename");
}

protected function htmlOutputPath(): string
Expand Down

0 comments on commit 8030aad

Please sign in to comment.