Skip to content

Commit 21dcf2f

Browse files
authored
Merge pull request #5 from bbrala/feature/generate-openapi-json
Allow generating json format
2 parents 05d09dd + ac31785 commit 21dcf2f

6 files changed

Lines changed: 31 additions & 15 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"require-dev": {
2525
"laravel-json-api/laravel": "^1.0",
2626
"orchestra/testbench": "^6.9",
27-
"phpunit/phpunit": "^9.5"
27+
"phpunit/phpunit": "^9.5",
28+
"ext-json": "*"
2829
},
2930
"autoload": {
3031
"psr-4": {

src/Commands/GenerateCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class GenerateCommand extends Command
1414
*
1515
* @var string
1616
*/
17-
protected $signature = 'jsonapi:openapi:generate {serverKey}';
17+
protected $signature = 'jsonapi:openapi:generate {serverKey} {format=json}';
18+
1819

1920
/**
2021
* The console command description.
@@ -31,10 +32,11 @@ class GenerateCommand extends Command
3132
public function handle()
3233
{
3334
$serverKey = $this->argument('serverKey');
35+
$format = $this->argument('format');
3436

3537
$this->info('Generating Open API spec...');
3638
try {
37-
GeneratorFacade::generate($serverKey);
39+
GeneratorFacade::generate($serverKey, $format);
3840
} catch (ValidationException $exception) {
3941
$this->error('Validation failed');
4042
$this->line('Errors:');
@@ -50,10 +52,10 @@ public function handle()
5052
return 1;
5153
}
5254

53-
$this->line('Complete! /storage/app/'.$serverKey.'_openapi.yaml');
55+
$this->line('Complete! /storage/app/'.$serverKey.'_openapi.' . $format);
5456
$this->newLine();
5557
$this->line('Run the following to see your API docs');
56-
$this->info('speccy serve storage/app/'.$serverKey.'_openapi.yaml');
58+
$this->info('speccy serve storage/app/'.$serverKey.'_openapi.' . $format);
5759
$this->newLine();
5860

5961
return 0;

src/Facades/GeneratorFacade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* Class GeneratorFacade
9-
* @method static bool generate(string $serverKey)
9+
* @method static bool generate(string $serverKey, string $format = 'yaml')
1010
*/
1111
class GeneratorFacade extends Facade
1212
{

src/OpenApiGenerator.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@ class OpenApiGenerator
1111
/**
1212
* @throws \GoldSpecDigital\ObjectOrientedOAS\Exceptions\ValidationException
1313
*/
14-
public function generate(string $serverKey): string
14+
public function generate(string $serverKey, string $format = 'yaml'): string
1515
{
1616

1717
$generator = new Generator($serverKey);
1818
$openapi = $generator->generate();
1919

20-
$openapi->validate();
21-
22-
$yaml = Yaml::dump($openapi->toArray());
23-
20+
$openapi->validate();
2421

22+
if ($format === 'yaml') {
23+
$output = Yaml::dump($openapi->toArray());
2524
// Save to storage
26-
Storage::put($serverKey.'_openapi.yaml', $yaml);
25+
Storage::put($serverKey.'_openapi.yaml', $output);
26+
} elseif ($format === 'json') {
27+
$output = json_encode($openapi->toArray(), JSON_PRETTY_PRINT);
28+
Storage::put($serverKey.'_openapi.json', $output);
29+
}
2730

28-
return $yaml;
31+
return $output;
2932
}
3033
}

src/Route.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function __construct(Server $server, IlluminateRoute $route)
9696
}
9797

9898
$this->setUriForRoute();
99-
99+
100100
[$controller, $method] = explode('@', $this->route->getActionName(), 2);
101101

102102
$this->controller = $controller;

tests/Feature/GenerateTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,22 @@ protected function setUp(): void
2222

2323
public function test_spec_is_yaml()
2424
{
25-
$openapiYaml = GeneratorFacade::generate('v1');
25+
$openapiYaml = GeneratorFacade::generate('v1', 'yaml');
2626

2727
$spec = Yaml::parse($openapiYaml);
2828

2929
$this->assertEquals('My JSON:API', $spec['info']['title']);
3030
}
3131

32+
public function test_spec_is_json()
33+
{
34+
$output = GeneratorFacade::generate('v1', 'json');
35+
36+
$spec = json_decode($output, true);
37+
38+
$this->assertEquals('My JSON:API', $spec['info']['title']);
39+
}
40+
3241
public function test_spec_file_generated()
3342
{
3443
GeneratorFacade::generate('v1');
@@ -49,6 +58,7 @@ public function test_url_is_properly_parsed()
4958
$spec = Yaml::parse($openapiYaml);
5059

5160
$this->assertArrayHasKey('/posts', $spec['paths'], 'Path to resource is not replaced correctly.');
61+
5262
$this->assertArrayHasKey('/posts/{post}/relationships/author', $spec['paths'], 'Path to resource is not replaced correctly.');
5363

5464
$this->assertEquals('http://localhost/api/v1', $spec['servers'][0]['variables']['serverUrl']['default']);

0 commit comments

Comments
 (0)