Skip to content

Commit

Permalink
Fix OpenApi annotation parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Mar 18, 2019
1 parent 4bf882f commit 57ff0f8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ parameters:

# Ignore bad php internal functions behavior
- '#^Parameter \#1 \$haystack of static method Nette\\Utils\\Strings\:\:contains\(\) expects string, string\|false given\.$#'
- message: '#^Method Apitte\\Core\\Annotation\\Controller\\OpenApi\:\:purifyDocblock\(\) should return string but returns string\|null\.$#'
path: %currentWorkingDirectory%/src/Annotation/Controller/OpenApi.php
15 changes: 9 additions & 6 deletions src/Annotation/Controller/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@
final class OpenApi
{

/** @var mixed[] */
/** @var string */
private $data;

/**
* @param mixed[] $data
*/
public function __construct(array $data)
{
$this->data = $data;
$this->data = $this->purifyDocblock($data['value']);
}

/**
* @return mixed[]
*/
public function getData(): array
public function getData(): string
{
return $this->data;
}

private function purifyDocblock(string $docblock): string
{
// Removes useless whitespace and * from start of every line
return preg_replace('#\s*\*\/$|^\s*\*\s{0,1}|^\/\*{1,2}#m', '', $docblock);
}

}
3 changes: 2 additions & 1 deletion src/DI/Loader/DoctrineAnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Apitte\Core\Schema\Builder\SchemaBuilder;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Nette\Neon\Neon;
use Nette\Reflection\ClassType;

final class DoctrineAnnotationLoader extends AbstractContainerLoader
Expand Down Expand Up @@ -294,7 +295,7 @@ protected function parseControllerMethodsAnnotations(Controller $controller, Cla
// Parse @OpenApi ================
if (get_class($annotation) === OpenApi::class) {
/** @var OpenApi $annotation */
$schemaMethod->setOpenApi($annotation->getData());
$schemaMethod->setOpenApi(Neon::decode($annotation->getData()) ?? []);
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/DI/ApiExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test(function (): void {

/** @var Schema $schema */
$schema = $container->getService('api.core.schema');
Assert::count(3, $schema->getEndpoints());
Assert::count(4, $schema->getEndpoints());
Assert::equal(['GET'], $schema->getEndpoints()[0]->getMethods());
Assert::equal('/api/v1/foobar/baz1', $schema->getEndpoints()[0]->getMask());
Assert::equal('#/api/v1/foobar/baz1$#', $schema->getEndpoints()[0]->getPattern());
Expand Down
10 changes: 9 additions & 1 deletion tests/cases/DI/Loader/DoctrineAnnotationLoader.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test(function (): void {
Assert::equal(FoobarController::class, $controller->getClass());
Assert::equal('/foobar', $controller->getPath());

Assert::count(3, $controller->getMethods());
Assert::count(4, $controller->getMethods());

Assert::equal('baz1', $controller->getMethods()['baz1']->getName());
Assert::equal('/baz1', $controller->getMethods()['baz1']->getPath());
Expand All @@ -68,6 +68,14 @@ test(function (): void {
Assert::equal('/baz2', $controller->getMethods()['baz2']->getPath());
Assert::equal(['GET', 'POST'], $controller->getMethods()['baz2']->getMethods());

Assert::equal(
[
'foo' => ['bar' => 'baz'],
'lorem' => ['ipsum', 'dolor', 'sit', 'amet'],
],
$controller->getMethods()['openapi']->getOpenApi()
);

Assert::equal(['testapi'], $controller->getGroupIds());
Assert::equal(['/api', '/v1'], $controller->getGroupPaths());
});
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/Controllers/FoobarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Apitte\Core\Annotation\Controller\ControllerPath;
use Apitte\Core\Annotation\Controller\Method;
use Apitte\Core\Annotation\Controller\OpenApi;
use Apitte\Core\Annotation\Controller\Path;
use Apitte\Core\Http\ApiRequest;
use Apitte\Core\Http\ApiResponse;
Expand Down Expand Up @@ -38,6 +39,23 @@ public function baz3(ApiRequest $request, ApiResponse $response): void
{
}

/**
* @Path("/openapi")
* @Method({"PUT"})
* @OpenApi("
* foo:
* bar: baz
* lorem:
* - ipsum
* - dolor
* - sit
* - amet
* ")
*/
public function openapi(ApiRequest $request, ApiResponse $response): void
{
}

public function getData1(): void
{
// Skip this method
Expand Down

0 comments on commit 57ff0f8

Please sign in to comment.