Skip to content

Allow customising swagger responses #54

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require-dev": {
"php": ">=7.0",
"php": ">=7.1",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because variable unpacking is only available in 7.1 onwards, eg

[$isDeprecated, $summary, $description] = $this->parseActionDocBlock($docBlock);

"orchestra/testbench": "~4.0",
"phpunit/phpunit": "^8.0|^9.0",
"laravel/passport": "^8.0"
Expand Down
10 changes: 9 additions & 1 deletion src/GenerateSwaggerDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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'))
Expand Down
17 changes: 11 additions & 6 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ class Generator

protected $config;
protected $routeFilter;
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;
}
Expand Down Expand Up @@ -137,20 +139,23 @@ protected function generatePath()
'summary' => $summary,
'description' => $description,
'deprecated' => $isDeprecated,
'responses' => [
'200' => [
'description' => 'OK',
],
],
];

$this->addResponseDefinition();
$this->addActionParameters();

if ($this->hasSecurityDefinitions) {
$this->addActionScopes();
}
}

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() ?: [];
Expand Down
18 changes: 18 additions & 0 deletions src/Responses/ResponseGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Mtrajano\LaravelSwagger\Responses;

class ResponseGenerator implements ResponseGeneratorInterface
{
/**
* @inheritDoc
*/
public function getResponses($uri, $method, $actionInstance)
{
return [
'200' => [
'description' => 'OK',
],
];
}
}
14 changes: 14 additions & 0 deletions src/Responses/ResponseGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Mtrajano\LaravelSwagger\Responses;

interface ResponseGeneratorInterface
{
/**
* @param string $uri
* @param string $method
* @param ?\ReflectionMethod $actionInstance
* @return array
*/
public function getResponses($uri, $method, $actionInstance);
}
22 changes: 19 additions & 3 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

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
{
protected $config;

protected $generator;

protected $responseGenerator;

protected $endpoints = [
'/users',
'/users/{id}',
Expand All @@ -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
);
}

Expand Down Expand Up @@ -197,6 +209,9 @@ public function testRouteData($paths)
Please read the documentation for more information
EOD;

//Allow running tests on windows
$expectedPostDescription = str_replace(PHP_EOL, "\n", $expectedPostDescription);

$this->assertArrayHasKey('summary', $paths['/users']['get']);
$this->assertArrayHasKey('description', $paths['/users']['get']);
$this->assertArrayHasKey('responses', $paths['/users']['get']);
Expand Down Expand Up @@ -282,7 +297,8 @@ public function testFiltersRoutes($routeFilter, $expectedRoutes)
{
$this->generator = new Generator(
$this->config,
$routeFilter
$routeFilter,
$this->responseGenerator
);

$docs = $this->generator->generate();
Expand All @@ -306,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();
}
}