Skip to content

Commit

Permalink
Merge pull request #5 from bbrala/feature/generate-openapi-json
Browse files Browse the repository at this point in the history
Allow generating json format
  • Loading branch information
captnCC authored Aug 10, 2021
2 parents 05d09dd + ac31785 commit 21dcf2f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 6 additions & 4 deletions src/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:');
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/GeneratorFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
17 changes: 10 additions & 7 deletions src/OpenApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function __construct(Server $server, IlluminateRoute $route)
}

$this->setUriForRoute();

[$controller, $method] = explode('@', $this->route->getActionName(), 2);

$this->controller = $controller;
Expand Down
12 changes: 11 additions & 1 deletion tests/Feature/GenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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']);
Expand Down

0 comments on commit 21dcf2f

Please sign in to comment.