diff --git a/composer.json b/composer.json index e61f4dc..4a08960 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "require-dev": { "laravel-json-api/laravel": "^1.0", "orchestra/testbench": "^6.9", - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "ext-json": "*" }, "autoload": { "psr-4": { diff --git a/src/Commands/GenerateCommand.php b/src/Commands/GenerateCommand.php index a121a3d..d37d4dc 100644 --- a/src/Commands/GenerateCommand.php +++ b/src/Commands/GenerateCommand.php @@ -14,7 +14,8 @@ class GenerateCommand extends Command * * @var string */ - protected $signature = 'jsonapi:openapi:generate {serverKey}'; + protected $signature = 'jsonapi:openapi:generate {serverKey} {format=json}'; + /** * The console command description. @@ -31,10 +32,11 @@ class GenerateCommand extends Command public function handle() { $serverKey = $this->argument('serverKey'); + $format = $this->argument('format'); $this->info('Generating Open API spec...'); try { - GeneratorFacade::generate($serverKey); + GeneratorFacade::generate($serverKey, $format); } catch (ValidationException $exception) { $this->error('Validation failed'); $this->line('Errors:'); @@ -50,10 +52,10 @@ public function handle() return 1; } - $this->line('Complete! /storage/app/'.$serverKey.'_openapi.yaml'); + $this->line('Complete! /storage/app/'.$serverKey.'_openapi.' . $format); $this->newLine(); $this->line('Run the following to see your API docs'); - $this->info('speccy serve storage/app/'.$serverKey.'_openapi.yaml'); + $this->info('speccy serve storage/app/'.$serverKey.'_openapi.' . $format); $this->newLine(); return 0; diff --git a/src/Facades/GeneratorFacade.php b/src/Facades/GeneratorFacade.php index 3eec8ff..115b2e9 100644 --- a/src/Facades/GeneratorFacade.php +++ b/src/Facades/GeneratorFacade.php @@ -6,7 +6,7 @@ /** * Class GeneratorFacade - * @method static bool generate(string $serverKey) + * @method static bool generate(string $serverKey, string $format = 'yaml') */ class GeneratorFacade extends Facade { diff --git a/src/OpenApiGenerator.php b/src/OpenApiGenerator.php index dfb4c47..1f3fc68 100644 --- a/src/OpenApiGenerator.php +++ b/src/OpenApiGenerator.php @@ -11,20 +11,23 @@ class OpenApiGenerator /** * @throws \GoldSpecDigital\ObjectOrientedOAS\Exceptions\ValidationException */ - public function generate(string $serverKey): string + public function generate(string $serverKey, string $format = 'yaml'): string { $generator = new Generator($serverKey); $openapi = $generator->generate(); - $openapi->validate(); - - $yaml = Yaml::dump($openapi->toArray()); - + $openapi->validate(); + if ($format === 'yaml') { + $output = Yaml::dump($openapi->toArray()); // Save to storage - Storage::put($serverKey.'_openapi.yaml', $yaml); + Storage::put($serverKey.'_openapi.yaml', $output); + } elseif ($format === 'json') { + $output = json_encode($openapi->toArray(), JSON_PRETTY_PRINT); + Storage::put($serverKey.'_openapi.json', $output); + } - return $yaml; + return $output; } } diff --git a/src/Route.php b/src/Route.php index ec1ca8b..e6812a6 100644 --- a/src/Route.php +++ b/src/Route.php @@ -96,7 +96,7 @@ public function __construct(Server $server, IlluminateRoute $route) } $this->setUriForRoute(); - + [$controller, $method] = explode('@', $this->route->getActionName(), 2); $this->controller = $controller; diff --git a/tests/Feature/GenerateTest.php b/tests/Feature/GenerateTest.php index cbeb87d..38cf334 100644 --- a/tests/Feature/GenerateTest.php +++ b/tests/Feature/GenerateTest.php @@ -22,13 +22,22 @@ protected function setUp(): void public function test_spec_is_yaml() { - $openapiYaml = GeneratorFacade::generate('v1'); + $openapiYaml = GeneratorFacade::generate('v1', 'yaml'); $spec = Yaml::parse($openapiYaml); $this->assertEquals('My JSON:API', $spec['info']['title']); } + public function test_spec_is_json() + { + $output = GeneratorFacade::generate('v1', 'json'); + + $spec = json_decode($output, true); + + $this->assertEquals('My JSON:API', $spec['info']['title']); + } + public function test_spec_file_generated() { GeneratorFacade::generate('v1'); @@ -49,6 +58,7 @@ public function test_url_is_properly_parsed() $spec = Yaml::parse($openapiYaml); $this->assertArrayHasKey('/posts', $spec['paths'], 'Path to resource is not replaced correctly.'); + $this->assertArrayHasKey('/posts/{post}/relationships/author', $spec['paths'], 'Path to resource is not replaced correctly.'); $this->assertEquals('http://localhost/api/v1', $spec['servers'][0]['variables']['serverUrl']['default']);