Skip to content

Commit c1886d6

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 c1886d6

File tree

19 files changed

+364
-334
lines changed

19 files changed

+364
-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: 35 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;
@@ -30,6 +27,11 @@ class OpenApiRouteLoader implements RouteLoaderInterface
3027
*/
3128
private $routeNames = [];
3229

30+
/**
31+
* @var null|Generator
32+
*/
33+
private $generator = null;
34+
3335
/**
3436
* @var string
3537
*/
@@ -89,23 +91,30 @@ private function createOpenApi(): OpenApi
8991
return \OpenApi\scan($this->finder);
9092
}
9193

94+
if (null !== $this->generator) {
95+
return $this->generator->generate($this->finder);
96+
}
97+
9298
if (method_exists(Analysis::class, 'processors')) {
9399
$processors = array_filter(Analysis::processors(), static function ($processor): bool {
94100
// remove OperationId processor which would hash the controller starting in 3.2.2 breaking the default route name logic
95101
return !$processor instanceof OperationId && !$processor instanceof DocBlockDescriptions;
96102
});
97103

98-
return (new Generator())->setProcessors($processors)->generate($this->finder);
104+
$this->generator = (new Generator())->setProcessors($this->filterProcessors($processors));
105+
106+
return $this->generator->generate($this->finder);
99107
}
100108

101-
$analyser = new ReflectionAnalyser([
102-
new AttributeAnnotationFactory(),
103-
new DocBlockAnnotationFactory()]
109+
$this->generator = new Generator();
110+
111+
$this->generator->setProcessors(
112+
$this->filterProcessors(
113+
$this->generator->getProcessors()
114+
)
104115
);
105116

106-
return (new Generator())
107-
->setAnalyser($analyser)
108-
->generate($this->finder);
117+
return $this->generator->generate($this->finder);
109118
}
110119

111120
/**
@@ -142,8 +151,14 @@ private function createRoute(Operation $operation, string $controller, FormatSuf
142151
}
143152
if (self::$openApiUndefined !== $operation->parameters) {
144153
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);
154+
if ('path' === $parameter->in && self::$openApiUndefined !== $parameter->schema) {
155+
if (self::$openApiUndefined !== $parameter->schema->pattern) {
156+
$route->setRequirement($parameter->name, $parameter->schema->pattern);
157+
}
158+
159+
if (self::$openApiUndefined !== $parameter->schema->enum) {
160+
$route->setRequirement($parameter->name, implode('|', $parameter->schema->enum));
161+
}
147162
}
148163
}
149164
}
@@ -204,4 +219,12 @@ private function getDefaultRouteName(string $controller): string
204219

205220
return $name;
206221
}
222+
223+
224+
private function filterProcessors(array $processors): array
225+
{
226+
return array_filter($processors, static function ($processor): bool {
227+
return !$processor instanceof OperationId && !$processor instanceof DocBlockDescriptions;
228+
});
229+
}
207230
}

0 commit comments

Comments
 (0)