Skip to content

Commit e041075

Browse files
committed
Add output and pretty config options for OpenAPI generator
Output defaults to public_path('openapi.json') so the spec is web-accessible. Both output and pretty can be set in config or overridden via CLI flags.
1 parent 78e2f36 commit e041075

3 files changed

Lines changed: 30 additions & 18 deletions

File tree

config/api-toolkit.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
'version' => '1.0.0',
5858
'description' => '',
5959

60+
'output' => public_path('openapi.json'),
61+
'pretty' => false,
62+
6063
'servers' => [
6164
['url' => env('APP_URL', 'http://localhost') . '/api'],
6265
],

src/Console/GenerateOpenApiCommand.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
final class GenerateOpenApiCommand extends Command
1313
{
1414
protected $signature = 'api-toolkit:openapi
15-
{--output=openapi.json : Output file path}
15+
{--output= : Output file path}
1616
{--pretty : Pretty print the JSON output}';
1717

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

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

45+
$pretty = $this->option('pretty') || (bool) ($openApiConfig['pretty'] ?? false);
4546
$flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
4647

47-
if ($this->option('pretty')) {
48+
if ($pretty) {
4849
$flags |= JSON_PRETTY_PRINT;
4950
}
5051

5152
$json = json_encode($document, $flags);
52-
$outputPath = $this->option('output');
53+
$outputPath = $this->option('output')
54+
?? $openApiConfig['output']
55+
?? public_path('openapi.json');
5356

54-
file_put_contents(base_path($outputPath), $json);
57+
if (file_put_contents($outputPath, $json) === false) {
58+
$this->error("Failed to write to {$outputPath}");
59+
60+
return self::FAILURE;
61+
}
5562

5663
$this->info("OpenAPI spec written to {$outputPath}");
5764

tests/Feature/Console/GenerateOpenApiCommandTest.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
afterEach(function () {
1212
$files = [
13-
base_path('openapi.json'),
14-
base_path('custom-output.json'),
13+
public_path('openapi.json'),
14+
public_path('custom-output.json'),
1515
];
1616

1717
foreach ($files as $file) {
@@ -27,35 +27,37 @@
2727
->assertSuccessful()
2828
;
2929

30-
expect(base_path('openapi.json'))->not->toBeFile();
30+
expect(public_path('openapi.json'))->not->toBeFile();
3131
});
3232

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

3636
$this->artisan('api-toolkit:openapi')
3737
->expectsOutputToContain('endpoint(s)')
38-
->expectsOutputToContain('OpenAPI spec written to openapi.json')
38+
->expectsOutputToContain('OpenAPI spec written to')
3939
->assertSuccessful()
4040
;
4141

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

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

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

51-
$this->artisan('api-toolkit:openapi', ['--output' => 'custom-output.json'])
52-
->expectsOutputToContain('OpenAPI spec written to custom-output.json')
51+
$customPath = public_path('custom-output.json');
52+
53+
$this->artisan('api-toolkit:openapi', ['--output' => $customPath])
54+
->expectsOutputToContain('OpenAPI spec written to')
5355
->assertSuccessful()
5456
;
5557

56-
expect(base_path('custom-output.json'))->toBeFile();
58+
expect($customPath)->toBeFile();
5759

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

@@ -66,7 +68,7 @@
6668
->assertSuccessful()
6769
;
6870

69-
$raw = file_get_contents(base_path('openapi.json'));
71+
$raw = file_get_contents(public_path('openapi.json'));
7072
expect($raw)->toContain("\n");
7173
expect($raw)->toContain(' ');
7274
});
@@ -78,7 +80,7 @@
7880
->assertSuccessful()
7981
;
8082

81-
$raw = file_get_contents(base_path('openapi.json'));
83+
$raw = file_get_contents(public_path('openapi.json'));
8284
expect($raw)->not->toContain("\n");
8385
});
8486

@@ -100,7 +102,7 @@
100102
->assertSuccessful()
101103
;
102104

103-
$content = json_decode(file_get_contents(base_path('openapi.json')), true);
105+
$content = json_decode(file_get_contents(public_path('openapi.json')), true);
104106
expect($content['info']['title'])->toBe('My Custom API');
105107
expect($content['info']['version'])->toBe('2.5.0');
106108
expect($content['info']['description'])->toBe('Custom description');
@@ -119,7 +121,7 @@
119121
->assertSuccessful()
120122
;
121123

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

0 commit comments

Comments
 (0)