From 576b1994a713543c4eaeb6d43271e331530a0b2c Mon Sep 17 00:00:00 2001 From: princejohnsantillan Date: Tue, 28 Nov 2023 17:05:29 +0800 Subject: [PATCH 1/3] Add command to export specification json --- .gitignore | 1 + config/scramble.php | 5 +++ src/Console/Commands/ExportSpecifications.php | 44 +++++++++++++++++++ src/ScrambleServiceProvider.php | 2 + .../ExportSpecificationsCommandTest.php | 16 +++++++ 5 files changed, 68 insertions(+) create mode 100644 src/Console/Commands/ExportSpecifications.php create mode 100644 tests/Console/Commands/ExportSpecificationsCommandTest.php diff --git a/.gitignore b/.gitignore index 6ec77e4e..63dcf8ad 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ phpstan.neon testbench.yaml vendor node_modules +*-test.json diff --git a/config/scramble.php b/config/scramble.php index c793302c..1afe0c40 100644 --- a/config/scramble.php +++ b/config/scramble.php @@ -15,6 +15,11 @@ */ 'api_domain' => null, + /** + * The path where your OpenAPI specification will be exported. + */ + 'export_path' => 'api.json', + 'info' => [ /* * API version. diff --git a/src/Console/Commands/ExportSpecifications.php b/src/Console/Commands/ExportSpecifications.php new file mode 100644 index 00000000..baf9f216 --- /dev/null +++ b/src/Console/Commands/ExportSpecifications.php @@ -0,0 +1,44 @@ +hasOption('path') + ? $this->option('path') + : config('scramble.export_path', 'api.json'); + + File::put($filename, $specifications); + + $this->info("OpenAPI specifications exported to {$filename}."); + } +} diff --git a/src/ScrambleServiceProvider.php b/src/ScrambleServiceProvider.php index a15f5537..19aeada4 100644 --- a/src/ScrambleServiceProvider.php +++ b/src/ScrambleServiceProvider.php @@ -2,6 +2,7 @@ namespace Dedoc\Scramble; +use Dedoc\Scramble\Console\Commands\ExportSpecifications; use Dedoc\Scramble\Extensions\ExceptionToResponseExtension; use Dedoc\Scramble\Extensions\OperationExtension; use Dedoc\Scramble\Extensions\TypeToSchemaExtension; @@ -49,6 +50,7 @@ public function configurePackage(Package $package): void ->name('scramble') ->hasConfigFile() ->hasRoute('web') + ->hasCommand(ExportSpecifications::class) ->hasViews('scramble'); $this->app->singleton(FileParser::class, function () { diff --git a/tests/Console/Commands/ExportSpecificationsCommandTest.php b/tests/Console/Commands/ExportSpecificationsCommandTest.php new file mode 100644 index 00000000..e7b4ba19 --- /dev/null +++ b/tests/Console/Commands/ExportSpecificationsCommandTest.php @@ -0,0 +1,16 @@ +assertExitCode(0); + + expect(File::json($filepath))->toBe($generator()); +}); From f309e7120686155b5bb4f44a4ffaf8c1370238ba Mon Sep 17 00:00:00 2001 From: princejohnsantillan Date: Mon, 18 Dec 2023 05:29:22 +0800 Subject: [PATCH 2/3] Fix issue with hasOption always return true --- src/Console/Commands/ExportSpecifications.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Console/Commands/ExportSpecifications.php b/src/Console/Commands/ExportSpecifications.php index baf9f216..fc6c5e28 100644 --- a/src/Console/Commands/ExportSpecifications.php +++ b/src/Console/Commands/ExportSpecifications.php @@ -33,9 +33,7 @@ public function handle(Generator $generator): void $specifications = json_encode($generator()); /** @var string filename */ - $filename = $this->hasOption('path') - ? $this->option('path') - : config('scramble.export_path', 'api.json'); + $filename = $this->option('path') ?? config('scramble.export_path', 'api.json'); File::put($filename, $specifications); From 59b287ab3c09f1f5519746ae2f63c10a383f32ee Mon Sep 17 00:00:00 2001 From: princejohnsantillan Date: Tue, 26 Dec 2023 16:25:15 +0800 Subject: [PATCH 3/3] fix broken tests --- tests/Console/Commands/ExportSpecificationsCommandTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Console/Commands/ExportSpecificationsCommandTest.php b/tests/Console/Commands/ExportSpecificationsCommandTest.php index e7b4ba19..792d3a3e 100644 --- a/tests/Console/Commands/ExportSpecificationsCommandTest.php +++ b/tests/Console/Commands/ExportSpecificationsCommandTest.php @@ -1,7 +1,6 @@ assertExitCode(0); - expect(File::json($filepath))->toBe($generator()); + $json_data = json_decode(file_get_contents($filepath), true); + + expect($json_data)->toBe($generator()); });