Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/api-toolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
'version' => '1.0.0',
'description' => '',

'output' => [
'path' => public_path('openapi.json'),
'pretty' => false,
],

'servers' => [
['url' => env('APP_URL', 'http://localhost')],
],
Expand Down
12 changes: 8 additions & 4 deletions src/Console/GenerateOpenApiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class GenerateOpenApiCommand extends Command
{
protected $signature = 'api-toolkit:openapi
{--output=openapi.json : Output file path}
{--output= : Output file path}
{--pretty : Pretty print the JSON output}';

protected $description = 'Generate an OpenAPI 3.1 specification from your API Toolkit resources';
Expand Down Expand Up @@ -42,16 +42,20 @@ public function handle(RouteScanner $scanner, Config $config): int

$document = $builder->build($endpoints);

$outputConfig = $openApiConfig['output'] ?? [];
$pretty = $this->option('pretty') || (bool) ($outputConfig['pretty'] ?? false);
$flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

if ($this->option('pretty')) {
if ($pretty) {
$flags |= JSON_PRETTY_PRINT;
}

$json = json_encode($document, $flags);
$outputPath = $this->option('output');
$outputPath = $this->option('output')
?? $outputConfig['path']
?? public_path('openapi.json');

if (file_put_contents(base_path($outputPath), $json) === false) {
if (file_put_contents($outputPath, $json) === false) {
$this->error("Failed to write to {$outputPath}");

return self::FAILURE;
Expand Down
30 changes: 16 additions & 14 deletions tests/Feature/Console/GenerateOpenApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

afterEach(function () {
$files = [
base_path('openapi.json'),
base_path('custom-output.json'),
public_path('openapi.json'),
public_path('custom-output.json'),
];

foreach ($files as $file) {
Expand All @@ -27,35 +27,37 @@
->assertSuccessful()
;

expect(base_path('openapi.json'))->not->toBeFile();
expect(public_path('openapi.json'))->not->toBeFile();
});

it('generates openapi.json with default output path', function () {
registerRoutes();

$this->artisan('api-toolkit:openapi')
->expectsOutputToContain('endpoint(s)')
->expectsOutputToContain('OpenAPI spec written to openapi.json')
->expectsOutputToContain('OpenAPI spec written to')
->assertSuccessful()
;

expect(base_path('openapi.json'))->toBeFile();
expect(public_path('openapi.json'))->toBeFile();

$content = json_decode(file_get_contents(base_path('openapi.json')), true);
$content = json_decode(file_get_contents(public_path('openapi.json')), true);
expect($content['openapi'])->toBe('3.1.0');
});

it('generates to custom output path', function () {
registerRoutes();

$this->artisan('api-toolkit:openapi', ['--output' => 'custom-output.json'])
->expectsOutputToContain('OpenAPI spec written to custom-output.json')
$customPath = public_path('custom-output.json');

$this->artisan('api-toolkit:openapi', ['--output' => $customPath])
->expectsOutputToContain('OpenAPI spec written to')
->assertSuccessful()
;

expect(base_path('custom-output.json'))->toBeFile();
expect($customPath)->toBeFile();

$content = json_decode(file_get_contents(base_path('custom-output.json')), true);
$content = json_decode(file_get_contents($customPath), true);
expect($content['openapi'])->toBe('3.1.0');
});

Expand All @@ -66,7 +68,7 @@
->assertSuccessful()
;

$raw = file_get_contents(base_path('openapi.json'));
$raw = file_get_contents(public_path('openapi.json'));
expect($raw)->toContain("\n");
expect($raw)->toContain(' ');
});
Expand All @@ -78,7 +80,7 @@
->assertSuccessful()
;

$raw = file_get_contents(base_path('openapi.json'));
$raw = file_get_contents(public_path('openapi.json'));
expect($raw)->not->toContain("\n");
});

Expand All @@ -100,7 +102,7 @@
->assertSuccessful()
;

$content = json_decode(file_get_contents(base_path('openapi.json')), true);
$content = json_decode(file_get_contents(public_path('openapi.json')), true);
expect($content['info']['title'])->toBe('My Custom API');
expect($content['info']['version'])->toBe('2.5.0');
expect($content['info']['description'])->toBe('Custom description');
Expand All @@ -119,7 +121,7 @@
->assertSuccessful()
;

$content = json_decode(file_get_contents(base_path('openapi.json')), true);
$content = json_decode(file_get_contents(public_path('openapi.json')), true);
expect($content['info']['title'])->toBe('Fallback App');
});

Expand Down