From 8cca4e0f318834daf645cbbae35ee68facd460fb Mon Sep 17 00:00:00 2001 From: "markl@room40.com.au" Date: Fri, 30 Oct 2020 11:15:42 +1000 Subject: [PATCH 1/3] Allow customising responses in swagger --- composer.json | 2 +- src/Generator.php | 18 ++++++++++++------ src/Responses/ResponseGenerator.php | 17 +++++++++++++++++ src/Responses/ResponseGeneratorInterface.php | 13 +++++++++++++ tests/GeneratorTest.php | 3 +++ 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 src/Responses/ResponseGenerator.php create mode 100644 src/Responses/ResponseGeneratorInterface.php diff --git a/composer.json b/composer.json index 39f8379..0b8e4dd 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require-dev": { - "php": ">=7.0", + "php": ">=7.1", "orchestra/testbench": "~4.0", "phpunit/phpunit": "^8.0|^9.0", "laravel/passport": "^8.0" diff --git a/src/Generator.php b/src/Generator.php index 804bad3..d74a79e 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -15,16 +15,19 @@ class Generator protected $config; protected $routeFilter; + /** @var Responses\ResponseGeneratorInterface */ + protected $responseGenerator; protected $docs; protected $route; protected $method; protected $docParser; protected $hasSecurityDefinitions; - public function __construct($config, $routeFilter = null) + public function __construct($config, $routeFilter = null, Responses\ResponseGeneratorInterface $responseGenerator = null) { $this->config = $config; $this->routeFilter = $routeFilter; + $this->responseGenerator = $responseGenerator ?: new Responses\ResponseGenerator(); $this->docParser = DocBlockFactory::createInstance(); $this->hasSecurityDefinitions = false; } @@ -137,13 +140,9 @@ protected function generatePath() 'summary' => $summary, 'description' => $description, 'deprecated' => $isDeprecated, - 'responses' => [ - '200' => [ - 'description' => 'OK', - ], - ], ]; + $this->addResponseDefinition(); $this->addActionParameters(); if ($this->hasSecurityDefinitions) { @@ -151,6 +150,13 @@ protected function generatePath() } } + protected function addResponseDefinition() + { + $actionInstance = $this->getActionClassInstance(); + $responses = $this->responseGenerator->getResponses($this->route->uri(), $this->method, $actionInstance); + $this->docs['paths'][$this->route->uri()][$this->method]['responses'] = $responses; + } + protected function addActionParameters() { $rules = $this->getFormRules() ?: []; diff --git a/src/Responses/ResponseGenerator.php b/src/Responses/ResponseGenerator.php new file mode 100644 index 0000000..d882a38 --- /dev/null +++ b/src/Responses/ResponseGenerator.php @@ -0,0 +1,17 @@ + [ + 'description' => 'OK', + ], + ]; + } +} diff --git a/src/Responses/ResponseGeneratorInterface.php b/src/Responses/ResponseGeneratorInterface.php new file mode 100644 index 0000000..24333e9 --- /dev/null +++ b/src/Responses/ResponseGeneratorInterface.php @@ -0,0 +1,13 @@ +assertArrayHasKey('summary', $paths['/users']['get']); $this->assertArrayHasKey('description', $paths['/users']['get']); $this->assertArrayHasKey('responses', $paths['/users']['get']); From 32a72499fcd3248d2a932e81079fa74f19a048aa Mon Sep 17 00:00:00 2001 From: "markl@room40.com.au" Date: Fri, 30 Oct 2020 11:46:46 +1000 Subject: [PATCH 2/3] Fixes --- src/GenerateSwaggerDoc.php | 10 +++++++++- src/Generator.php | 1 - tests/GeneratorTest.php | 19 ++++++++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/GenerateSwaggerDoc.php b/src/GenerateSwaggerDoc.php index 22ca2a7..6b10802 100644 --- a/src/GenerateSwaggerDoc.php +++ b/src/GenerateSwaggerDoc.php @@ -6,6 +6,14 @@ class GenerateSwaggerDoc extends Command { + protected $responseGenerator; + + public function __construct(Responses\ResponseGeneratorInterface $responseGenerator = null) + { + parent::__construct(); + $this->responseGenerator = $responseGenerator; + } + /** * The name and signature of the console command. * @@ -34,7 +42,7 @@ public function handle() $filter = $this->option('filter') ?: null; $file = $this->option('output') ?: null; - $docs = (new Generator($config, $filter))->generate(); + $docs = (new Generator($config, $filter, $this->responseGenerator))->generate(); $formattedDocs = (new FormatterManager($docs)) ->setFormat($this->option('format')) diff --git a/src/Generator.php b/src/Generator.php index d74a79e..39b7f0f 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -15,7 +15,6 @@ class Generator protected $config; protected $routeFilter; - /** @var Responses\ResponseGeneratorInterface */ protected $responseGenerator; protected $docs; protected $route; diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index e7165b1..224d2dc 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -2,8 +2,10 @@ namespace Mtrajano\LaravelSwagger\Tests; +use Illuminate\Contracts\Container\BindingResolutionException; use Mtrajano\LaravelSwagger\Generator; use Mtrajano\LaravelSwagger\LaravelSwaggerException; +use Mtrajano\LaravelSwagger\Responses\ResponseGeneratorInterface; class GeneratorTest extends TestCase { @@ -11,6 +13,8 @@ class GeneratorTest extends TestCase protected $generator; + protected $responseGenerator; + protected $endpoints = [ '/users', '/users/{id}', @@ -34,8 +38,16 @@ public function setUp(): void { parent::setUp(); + try { + $this->responseGenerator = $this->app->make(ResponseGeneratorInterface::class); + } catch (BindingResolutionException $e) { + $this->responseGenerator = null; + } + $this->generator = new Generator( - $this->config = config('laravel-swagger') + $this->config = config('laravel-swagger'), + null, + $this->responseGenerator ); } @@ -285,7 +297,8 @@ public function testFiltersRoutes($routeFilter, $expectedRoutes) { $this->generator = new Generator( $this->config, - $routeFilter + $routeFilter, + $this->responseGenerator ); $docs = $this->generator->generate(); @@ -309,6 +322,6 @@ private function getDocsWithNewConfig(array $config) { $config = array_merge($this->config, $config); - return (new Generator($config))->generate(); + return (new Generator($config, null, $this->responseGenerator))->generate(); } } From 2513264f506f2de3abad6ced3589f0d61b00da69 Mon Sep 17 00:00:00 2001 From: "markl@room40.com.au" Date: Fri, 30 Oct 2020 11:51:04 +1000 Subject: [PATCH 3/3] StyleCI changes --- src/Responses/ResponseGenerator.php | 1 + src/Responses/ResponseGeneratorInterface.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Responses/ResponseGenerator.php b/src/Responses/ResponseGenerator.php index d882a38..cfd39fa 100644 --- a/src/Responses/ResponseGenerator.php +++ b/src/Responses/ResponseGenerator.php @@ -1,4 +1,5 @@