Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to export specification json #277

Merged
merged 4 commits into from Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,3 +9,4 @@ phpstan.neon
testbench.yaml
vendor
node_modules
*-test.json
5 changes: 5 additions & 0 deletions config/scramble.php
Expand Up @@ -15,6 +15,11 @@
*/
'api_domain' => null,

/**
* The path where your OpenAPI specification will be exported.
*/
'export_path' => 'api.json',

'info' => [
/*
* API version.
Expand Down
42 changes: 42 additions & 0 deletions src/Console/Commands/ExportSpecifications.php
@@ -0,0 +1,42 @@
<?php

namespace Dedoc\Scramble\Console\Commands;

use Dedoc\Scramble\Generator;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class ExportSpecifications extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scramble:export
{--path= : The path where to save the exported json file}
';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Export the OpenAPI specifications to a json file.';

/**
* Execute the console command.
*/
public function handle(Generator $generator): void
{

$specifications = json_encode($generator());

/** @var string filename */
$filename = $this->option('path') ?? config('scramble.export_path', 'api.json');

File::put($filename, $specifications);

$this->info("OpenAPI specifications exported to {$filename}.");
}
}
2 changes: 2 additions & 0 deletions src/ScrambleServiceProvider.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand Down
16 changes: 16 additions & 0 deletions tests/Console/Commands/ExportSpecificationsCommandTest.php
@@ -0,0 +1,16 @@
<?php

use Dedoc\Scramble\Generator;
use Illuminate\Support\Facades\File;

use function Pest\Laravel\{artisan};

it('exports_the_specifications_to_a_specified_json_file', function () {
$filepath = 'api-test.json';

$generator = app(Generator::class);

artisan("scramble:export --path={$filepath}")->assertExitCode(0);

expect(File::json($filepath))->toBe($generator());
});