Skip to content

Commit

Permalink
Merge pull request #318 from shalvah/parse-response-tags-for-all-methods
Browse files Browse the repository at this point in the history
Parse response tags for all HTTP verbs
  • Loading branch information
shalvah authored Sep 11, 2018
2 parents af9da31 + 2117dcf commit a6de40a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 102 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

### Fixed
- Parse `@response` tags regardless of HTTP method

### Removed

Expand Down
58 changes: 56 additions & 2 deletions src/Mpociot/ApiDoc/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,61 @@ abstract public function getMethods($route);
*
* @return array
*/
abstract public function processRoute($route, $bindings = [], $withResponse = true);
public function processRoute($route, $bindings = [], $headers = [], $withResponse = true)
{
$routeDomain = $route->domain();
$routeAction = $route->getAction();
$routeGroup = $this->getRouteGroup($routeAction['uses']);
$routeDescription = $this->getRouteDescription($routeAction['uses']);
$showresponse = null;

// set correct route domain
$headers[] = "HTTP_HOST: {$routeDomain}";
$headers[] = "SERVER_NAME: {$routeDomain}";

$content = '';
$response = null;
$docblockResponse = $this->getDocblockResponse($routeDescription['tags']);
if ($docblockResponse) {
// we have a response from the docblock ( @response )
$response = $docblockResponse;
$showresponse = true;
$content = $response->getContent();
}
if (! $response) {
$transformerResponse = $this->getTransformerResponse($routeDescription['tags']);
if ($transformerResponse) {
// we have a transformer response from the docblock ( @transformer || @transformercollection )
$response = $transformerResponse;
$showresponse = true;
$content = $response->getContent();
}
}
if (! $response && $withResponse) {
try {
$response = $this->getRouteResponse($route, $bindings, $headers);
if ($response->headers->get('Content-Type') === 'application/json') {
$content = json_decode($response->getContent(), JSON_PRETTY_PRINT);
} else {
$content = $response->getContent();
}
} catch (\Exception $e) {
dump("Couldn't get response for route: ".implode(',', $this->getMethods($route)).'] '.$route->uri()."", $e);
}
}

return $this->getParameters([
'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))),
'resource' => $routeGroup,
'title' => $routeDescription['short'],
'description' => $routeDescription['long'],
'methods' => $this->getMethods($route),
'uri' => $this->getUri($route),
'parameters' => [],
'response' => $content,
'showresponse' => $showresponse,
], $routeAction, $bindings);
}

/**
* Prepares / Disables route middlewares.
Expand Down Expand Up @@ -179,7 +233,7 @@ protected function addRouteModelBindings($route, $bindings)
/**
* @param \Illuminate\Routing\Route $route
*
* @return string
* @return array
*/
protected function getRouteDescription($route)
{
Expand Down
36 changes: 0 additions & 36 deletions src/Mpociot/ApiDoc/Generators/DingoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,9 @@

namespace Mpociot\ApiDoc\Generators;

use Exception;

class DingoGenerator extends AbstractGenerator
{
/**
* @param \Illuminate\Routing\Route $route
* @param array $bindings
* @param array $headers
* @param bool $withResponse
*
* @return array
*/
public function processRoute($route, $bindings = [], $headers = [], $withResponse = true)
{
$response = '';

if ($withResponse) {
try {
$response = $this->getRouteResponse($route, $bindings, $headers);
} catch (Exception $e) {
}
}

$routeAction = $route->getAction();
$routeGroup = $this->getRouteGroup($routeAction['uses']);
$routeDescription = $this->getRouteDescription($routeAction['uses']);

return $this->getParameters([
'id' => md5($route->uri().':'.implode($route->getMethods())),
'resource' => $routeGroup,
'title' => $routeDescription['short'],
'description' => $routeDescription['long'],
'methods' => $route->getMethods(),
'uri' => $route->uri(),
'parameters' => [],
'response' => $response,
], $routeAction, $bindings);
}

/**
* Prepares / Disables route middlewares.
*
Expand Down
62 changes: 1 addition & 61 deletions src/Mpociot/ApiDoc/Generators/LaravelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mpociot\ApiDoc\Generators;

use Exception;
use ReflectionClass;
use League\Fractal\Manager;
use Illuminate\Routing\Route;
Expand Down Expand Up @@ -53,67 +54,6 @@ public function getMethods($route)
return array_diff($methods, ['HEAD']);
}

/**
* @param \Illuminate\Routing\Route $route
* @param array $bindings
* @param array $headers
* @param bool $withResponse
*
* @return array
*/
public function processRoute($route, $bindings = [], $headers = [], $withResponse = true)
{
$content = '';

$routeDomain = $route->domain();
$routeAction = $route->getAction();
$routeGroup = $this->getRouteGroup($routeAction['uses']);
$routeDescription = $this->getRouteDescription($routeAction['uses']);
$showresponse = null;

// set correct route domain
$headers[] = "HTTP_HOST: {$routeDomain}";
$headers[] = "SERVER_NAME: {$routeDomain}";

if ($withResponse) {
$response = null;
$docblockResponse = $this->getDocblockResponse($routeDescription['tags']);
if ($docblockResponse) {
// we have a response from the docblock ( @response )
$response = $docblockResponse;
$showresponse = true;
}
if (! $response) {
$transformerResponse = $this->getTransformerResponse($routeDescription['tags']);
if ($transformerResponse) {
// we have a transformer response from the docblock ( @transformer || @transformercollection )
$response = $transformerResponse;
$showresponse = true;
}
}
if (! $response) {
$response = $this->getRouteResponse($route, $bindings, $headers);
}
if ($response->headers->get('Content-Type') === 'application/json') {
$content = json_decode($response->getContent(), JSON_PRETTY_PRINT);
} else {
$content = $response->getContent();
}
}

return $this->getParameters([
'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))),
'resource' => $routeGroup,
'title' => $routeDescription['short'],
'description' => $routeDescription['long'],
'methods' => $this->getMethods($route),
'uri' => $this->getUri($route),
'parameters' => [],
'response' => $content,
'showresponse' => $showresponse,
], $routeAction, $bindings);
}

/**
* Prepares / Disables route middlewares.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/ApiDocGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,12 @@ public function testCustomFormRequestValidatorIsSupported()
public function testCanParseResponseTag()
{
RouteFacade::post('/responseTag', TestController::class.'@responseTag');
$route = new Route(['GET'], '/responseTag', ['uses' => TestController::class.'@responseTag']);
$route = new Route(['POST'], '/responseTag', ['uses' => TestController::class.'@responseTag']);
$parsed = $this->generator->processRoute($route);
$this->assertTrue(is_array($parsed));
$this->assertArrayHasKey('showresponse', $parsed);
$this->assertTrue($parsed['showresponse']);
$this->assertSame($parsed['response'], "{\n data: [],\n}");
$this->assertJsonStringEqualsJsonString(json_decode($parsed['response'], true), "{ \"data\": []}");
}

public function testCanParseTransformerTag()
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function skip()

/**
* @response {
* data: [],
* "data": []
*}
*/
public function responseTag()
Expand Down

0 comments on commit a6de40a

Please sign in to comment.