Skip to content
Merged
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
26 changes: 18 additions & 8 deletions src/Mpociot/ApiDoc/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,30 @@ public function processRoute($route, $bindings = [], $headers = [], $withRespons
$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);
}
}

$content = $this->getResponseContent($response);
return $this->getParameters([
'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))),
'resource' => $routeGroup,
Expand Down Expand Up @@ -665,4 +658,21 @@ protected function normalizeRule($rule)
return $rule;
}
}

/**
* @param $response
* @return mixed
*/
private function getResponseContent($response)
{
if (empty($response)) {
return '';
}
if ($response->headers->get('Content-Type') === 'application/json') {
$content = json_decode($response->getContent(), JSON_PRETTY_PRINT);
} else {
$content = $response->getContent();
}
return $content;
}
}
2 changes: 1 addition & 1 deletion tests/ApiDocGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public function testCanParseResponseTag()
$this->assertTrue(is_array($parsed));
$this->assertArrayHasKey('showresponse', $parsed);
$this->assertTrue($parsed['showresponse']);
$this->assertJsonStringEqualsJsonString(json_decode($parsed['response'], true), '{ "data": []}');
$this->assertJsonStringEqualsJsonString($parsed['response'], '{ "data": []}');
}

public function testCanParseTransformerTag()
Expand Down