Skip to content

Commit

Permalink
add php attributes support
Browse files Browse the repository at this point in the history
Tobion#12 add path parameter enum

Also need to add tests for new features like OA\PathParameter and enums, see example zircote/swagger-php#1060
  • Loading branch information
sonrac committed May 2, 2022
1 parent fa7fa35 commit 06df254
Show file tree
Hide file tree
Showing 19 changed files with 366 additions and 334 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ jobs:

- name: Run tests
continue-on-error: true
run: vendor/bin/simple-phpunit tests
run: vendor/bin/simple-phpunit
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.phpunit.result.cache
composer.lock
vendor/
docker-compose.override.yml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Version < 1.2 requires zircote/swagger-php 2.x which works with the OpenAPI Spec
So tobion/openapi-symfony-routing can be used with both OpenAPI v2 and v3 and composer will select the compatible one for your dependencies.
Route loading stays the same between those versions. You just need to update the annotations when migrating from OpenAPI v2 to v3.

Version >= 1.3 requires zircote/swagger-php 4.x which is compatible with the OpenAPI Specification version 3.0.1 and supports attributes. This package need php >= 8.1 for attributes support from zircote/swagger-php.
Version >= 1.3 requires zircote/swagger-php >=4.1 which is compatible with the OpenAPI Specification version 3.0.1 and supports attributes. This package need php >= 8.1 for attributes support from zircote/swagger-php.

## Basic Usage

Expand Down
10 changes: 10 additions & 0 deletions add_composer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

export COMPOSER_HOME=/tmp/composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
chmod +x composer.phar
mv composer.phar composer
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"symfony/finder": "^4.4|^5.0|^6.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/routing": "^4.4|^5.0|^6.0",
"zircote/swagger-php": "^3.0.3|^4.0"
"zircote/swagger-php": "^3.0.3|^4.1"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.2|^6.0"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
>
<testsuites>
<testsuite name="Test Suite">
<directory>tests/Annotations</directory>
<directory>tests/</directory>
</testsuite>
</testsuites>

Expand Down
49 changes: 37 additions & 12 deletions src/OpenApiRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Tobion\OpenApiSymfonyRouting;

use OpenApi\Analysers\AttributeAnnotationFactory;
use OpenApi\Analysers\DocBlockAnnotationFactory;
use OpenApi\Analysers\ReflectionAnalyser;
use OpenApi\Analysis;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Operation;
Expand All @@ -18,6 +15,8 @@
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

use function array_filter;

class OpenApiRouteLoader implements RouteLoaderInterface
{
/**
Expand All @@ -30,6 +29,11 @@ class OpenApiRouteLoader implements RouteLoaderInterface
*/
private $routeNames = [];

/**
* @var null|Generator
*/
private $generator = null;

/**
* @var string
*/
Expand Down Expand Up @@ -89,23 +93,30 @@ private function createOpenApi(): OpenApi
return \OpenApi\scan($this->finder);
}

if (null !== $this->generator) {
return $this->generator->generate($this->finder);
}

if (method_exists(Analysis::class, 'processors')) {
$processors = array_filter(Analysis::processors(), static function ($processor): bool {
// remove OperationId processor which would hash the controller starting in 3.2.2 breaking the default route name logic
return !$processor instanceof OperationId && !$processor instanceof DocBlockDescriptions;
});

return (new Generator())->setProcessors($processors)->generate($this->finder);
$this->generator = (new Generator())->setProcessors($this->filterProcessors($processors));

return $this->generator->generate($this->finder);
}

$analyser = new ReflectionAnalyser([
new AttributeAnnotationFactory(),
new DocBlockAnnotationFactory()]
$this->generator = new Generator();

$this->generator->setProcessors(
$this->filterProcessors(
$this->generator->getProcessors()
)
);

return (new Generator())
->setAnalyser($analyser)
->generate($this->finder);
return $this->generator->generate($this->finder);
}

/**
Expand Down Expand Up @@ -142,8 +153,14 @@ private function createRoute(Operation $operation, string $controller, FormatSuf
}
if (self::$openApiUndefined !== $operation->parameters) {
foreach ($operation->parameters as $parameter) {
if ('path' === $parameter->in && self::$openApiUndefined !== $parameter->schema && self::$openApiUndefined !== $parameter->schema->pattern) {
$route->setRequirement($parameter->name, $parameter->schema->pattern);
if ('path' === $parameter->in && self::$openApiUndefined !== $parameter->schema) {
if (self::$openApiUndefined !== $parameter->schema->pattern) {
$route->setRequirement($parameter->name, $parameter->schema->pattern);
}

if (self::$openApiUndefined !== $parameter->schema->enum) {
$route->setRequirement($parameter->name, implode('|', $parameter->schema->enum));
}
}
}
}
Expand Down Expand Up @@ -204,4 +221,12 @@ private function getDefaultRouteName(string $controller): string

return $name;
}


private function filterProcessors(array $processors): array
{
return array_filter($processors, static function ($processor): bool {
return !$processor instanceof OperationId && !$processor instanceof DocBlockDescriptions;
});
}
}
Loading

0 comments on commit 06df254

Please sign in to comment.