Skip to content

Commit 06df254

Browse files
committed
add php attributes support
#12 add path parameter enum Also need to add tests for new features like OA\PathParameter and enums, see example zircote/swagger-php#1060
1 parent fa7fa35 commit 06df254

File tree

19 files changed

+366
-334
lines changed

19 files changed

+366
-334
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ jobs:
7676

7777
- name: Run tests
7878
continue-on-error: true
79-
run: vendor/bin/simple-phpunit tests
79+
run: vendor/bin/simple-phpunit

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
.phpunit.result.cache
22
composer.lock
33
vendor/
4-
docker-compose.override.yml

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Version < 1.2 requires zircote/swagger-php 2.x which works with the OpenAPI Spec
1313
So tobion/openapi-symfony-routing can be used with both OpenAPI v2 and v3 and composer will select the compatible one for your dependencies.
1414
Route loading stays the same between those versions. You just need to update the annotations when migrating from OpenAPI v2 to v3.
1515

16-
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.
16+
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.
1717

1818
## Basic Usage
1919

add_composer.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
export COMPOSER_HOME=/tmp/composer
4+
5+
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
6+
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
7+
php composer-setup.php
8+
php -r "unlink('composer-setup.php');"
9+
chmod +x composer.phar
10+
mv composer.phar composer

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"symfony/finder": "^4.4|^5.0|^6.0",
1717
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
1818
"symfony/routing": "^4.4|^5.0|^6.0",
19-
"zircote/swagger-php": "^3.0.3|^4.0"
19+
"zircote/swagger-php": "^3.0.3|^4.1"
2020
},
2121
"require-dev": {
2222
"symfony/phpunit-bridge": "^5.2|^6.0"

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
>
1010
<testsuites>
1111
<testsuite name="Test Suite">
12-
<directory>tests/Annotations</directory>
12+
<directory>tests/</directory>
1313
</testsuite>
1414
</testsuites>
1515

src/OpenApiRouteLoader.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Tobion\OpenApiSymfonyRouting;
66

7-
use OpenApi\Analysers\AttributeAnnotationFactory;
8-
use OpenApi\Analysers\DocBlockAnnotationFactory;
9-
use OpenApi\Analysers\ReflectionAnalyser;
107
use OpenApi\Analysis;
118
use OpenApi\Annotations\OpenApi;
129
use OpenApi\Annotations\Operation;
@@ -18,6 +15,8 @@
1815
use Symfony\Component\Routing\Route;
1916
use Symfony\Component\Routing\RouteCollection;
2017

18+
use function array_filter;
19+
2120
class OpenApiRouteLoader implements RouteLoaderInterface
2221
{
2322
/**
@@ -30,6 +29,11 @@ class OpenApiRouteLoader implements RouteLoaderInterface
3029
*/
3130
private $routeNames = [];
3231

32+
/**
33+
* @var null|Generator
34+
*/
35+
private $generator = null;
36+
3337
/**
3438
* @var string
3539
*/
@@ -89,23 +93,30 @@ private function createOpenApi(): OpenApi
8993
return \OpenApi\scan($this->finder);
9094
}
9195

96+
if (null !== $this->generator) {
97+
return $this->generator->generate($this->finder);
98+
}
99+
92100
if (method_exists(Analysis::class, 'processors')) {
93101
$processors = array_filter(Analysis::processors(), static function ($processor): bool {
94102
// remove OperationId processor which would hash the controller starting in 3.2.2 breaking the default route name logic
95103
return !$processor instanceof OperationId && !$processor instanceof DocBlockDescriptions;
96104
});
97105

98-
return (new Generator())->setProcessors($processors)->generate($this->finder);
106+
$this->generator = (new Generator())->setProcessors($this->filterProcessors($processors));
107+
108+
return $this->generator->generate($this->finder);
99109
}
100110

101-
$analyser = new ReflectionAnalyser([
102-
new AttributeAnnotationFactory(),
103-
new DocBlockAnnotationFactory()]
111+
$this->generator = new Generator();
112+
113+
$this->generator->setProcessors(
114+
$this->filterProcessors(
115+
$this->generator->getProcessors()
116+
)
104117
);
105118

106-
return (new Generator())
107-
->setAnalyser($analyser)
108-
->generate($this->finder);
119+
return $this->generator->generate($this->finder);
109120
}
110121

111122
/**
@@ -142,8 +153,14 @@ private function createRoute(Operation $operation, string $controller, FormatSuf
142153
}
143154
if (self::$openApiUndefined !== $operation->parameters) {
144155
foreach ($operation->parameters as $parameter) {
145-
if ('path' === $parameter->in && self::$openApiUndefined !== $parameter->schema && self::$openApiUndefined !== $parameter->schema->pattern) {
146-
$route->setRequirement($parameter->name, $parameter->schema->pattern);
156+
if ('path' === $parameter->in && self::$openApiUndefined !== $parameter->schema) {
157+
if (self::$openApiUndefined !== $parameter->schema->pattern) {
158+
$route->setRequirement($parameter->name, $parameter->schema->pattern);
159+
}
160+
161+
if (self::$openApiUndefined !== $parameter->schema->enum) {
162+
$route->setRequirement($parameter->name, implode('|', $parameter->schema->enum));
163+
}
147164
}
148165
}
149166
}
@@ -204,4 +221,12 @@ private function getDefaultRouteName(string $controller): string
204221

205222
return $name;
206223
}
224+
225+
226+
private function filterProcessors(array $processors): array
227+
{
228+
return array_filter($processors, static function ($processor): bool {
229+
return !$processor instanceof OperationId && !$processor instanceof DocBlockDescriptions;
230+
});
231+
}
207232
}

0 commit comments

Comments
 (0)