Skip to content

Commit 9c47a51

Browse files
committed
allow format-suffix to be specified on any part of the openapi
so the config can be inherited which allows to change it globally or for different paths
1 parent be38dc5 commit 9c47a51

4 files changed

Lines changed: 77 additions & 34 deletions

File tree

src/FormatSuffixConfig.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tobion\OpenApiSymfonyRouting;
6+
7+
use Swagger\Annotations\AbstractAnnotation;
8+
9+
/**
10+
* @internal
11+
*/
12+
class FormatSuffixConfig
13+
{
14+
/**
15+
* @var bool
16+
*/
17+
public $enabled;
18+
19+
/**
20+
* @var string|null
21+
*/
22+
public $pattern;
23+
24+
public function __construct(
25+
bool $enabled,
26+
?string $pattern
27+
) {
28+
$this->enabled = $enabled;
29+
$this->pattern = $pattern;
30+
}
31+
32+
public static function fromAnnotation(AbstractAnnotation $annotation, ?self $parent = null): self
33+
{
34+
if (!isset($annotation->x['format-suffix'])) {
35+
return new self($parent ? $parent->enabled : false, $parent ? $parent->pattern : null);
36+
}
37+
38+
if (is_bool($annotation->x['format-suffix'])) {
39+
return new self($annotation->x['format-suffix'], $parent ? $parent->pattern : null);
40+
}
41+
42+
return new self(
43+
$annotation->x['format-suffix']['enabled'] ?? ($parent ? $parent->enabled : false),
44+
$annotation->x['format-suffix']['pattern'] ?? ($parent ? $parent->pattern : null)
45+
);
46+
}
47+
}

src/OpenApiRouteLoader.php

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,13 @@ class OpenApiRouteLoader implements RouteLoaderInterface
1717
*/
1818
private $finder;
1919

20-
/**
21-
* @var bool
22-
*/
23-
private $addFormatSuffix;
24-
25-
/**
26-
* @var string|null
27-
*/
28-
private $defaultFormatPattern;
29-
3020
/**
3121
* @var array<string, int>
3222
*/
3323
private $routeNames = [];
3424

3525
public function __construct(
36-
?Finder $finder = null,
37-
bool $addFormatSuffix = false,
38-
?string $defaultFormatPattern = null
26+
?Finder $finder = null
3927
) {
4028
if (null === $finder) {
4129
// try to use the symfony flex default src directory based on a composer install
@@ -49,55 +37,58 @@ public function __construct(
4937
}
5038

5139
$this->finder = $finder;
52-
$this->addFormatSuffix = $addFormatSuffix;
53-
$this->defaultFormatPattern = $defaultFormatPattern;
5440
}
5541

5642
public function __invoke(): RouteCollection
5743
{
5844
$fullSwagger = \Swagger\scan($this->finder);
5945
$routeCollection = new RouteCollection();
6046

47+
$globalFormatSuffixConfig = FormatSuffixConfig::fromAnnotation($fullSwagger);
48+
6149
foreach ($fullSwagger->paths as $path) {
62-
$this->addRouteFromSwaggerOperation($routeCollection, $path->get);
63-
$this->addRouteFromSwaggerOperation($routeCollection, $path->put);
64-
$this->addRouteFromSwaggerOperation($routeCollection, $path->post);
65-
$this->addRouteFromSwaggerOperation($routeCollection, $path->delete);
66-
$this->addRouteFromSwaggerOperation($routeCollection, $path->options);
67-
$this->addRouteFromSwaggerOperation($routeCollection, $path->head);
68-
$this->addRouteFromSwaggerOperation($routeCollection, $path->patch);
50+
$pathFormatSuffixConfig = FormatSuffixConfig::fromAnnotation($path, $globalFormatSuffixConfig);
51+
52+
$this->addRouteFromSwaggerOperation($routeCollection, $path->get, $pathFormatSuffixConfig);
53+
$this->addRouteFromSwaggerOperation($routeCollection, $path->put, $pathFormatSuffixConfig);
54+
$this->addRouteFromSwaggerOperation($routeCollection, $path->post, $pathFormatSuffixConfig);
55+
$this->addRouteFromSwaggerOperation($routeCollection, $path->delete, $pathFormatSuffixConfig);
56+
$this->addRouteFromSwaggerOperation($routeCollection, $path->options, $pathFormatSuffixConfig);
57+
$this->addRouteFromSwaggerOperation($routeCollection, $path->head, $pathFormatSuffixConfig);
58+
$this->addRouteFromSwaggerOperation($routeCollection, $path->patch, $pathFormatSuffixConfig);
6959
}
7060

7161
$this->routeNames = [];
7262

7363
return $routeCollection;
7464
}
7565

76-
private function addRouteFromSwaggerOperation(RouteCollection $routeCollection, ?Operation $operation): void
66+
private function addRouteFromSwaggerOperation(RouteCollection $routeCollection, ?Operation $operation, FormatSuffixConfig $parentFormatSuffixConfig): void
7767
{
7868
if (null === $operation) {
7969
return;
8070
}
8171

8272
$controller = $this->getControllerFromSwaggerOperation($operation);
8373
$name = $this->getRouteName($operation, $controller);
84-
$route = $this->createRoute($operation, $controller);
74+
$route = $this->createRoute($operation, $controller, $parentFormatSuffixConfig);
8575
$routeCollection->add($name, $route);
8676
}
8777

88-
private function createRoute(Operation $operation, string $controller): Route
78+
private function createRoute(Operation $operation, string $controller, FormatSuffixConfig $parentFormatSuffixConfig): Route
8979
{
90-
$formatSuffix = $operation->x['format-suffix'] ?? $this->addFormatSuffix;
91-
$path = $formatSuffix ? $operation->path.'.{_format}' : $operation->path;
80+
$formatSuffixConfig = FormatSuffixConfig::fromAnnotation($operation, $parentFormatSuffixConfig);
81+
82+
$path = $formatSuffixConfig->enabled ? $operation->path.'.{_format}' : $operation->path;
9283
$route = new Route($path);
9384
$route->setMethods($operation->method);
9485
$route->setDefault('_controller', $controller);
95-
if ($formatSuffix) {
96-
$formatPattern = $operation->x['format-pattern'] ?? $this->defaultFormatPattern;
86+
87+
if ($formatSuffixConfig->enabled) {
9788
$route->setDefault('_format', null);
9889

99-
if (null !== $formatPattern) {
100-
$route->setRequirement('_format', $formatPattern);
90+
if (null !== $formatSuffixConfig->pattern) {
91+
$route->setRequirement('_format', $formatSuffixConfig->pattern);
10192
}
10293
}
10394
if (null !== $operation->parameters) {

tests/Fixtures/FormatSuffix/Controller.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
* @SWG\Info(
1212
* title="My API",
1313
* version="1.0"
14-
* )
14+
* ),
15+
* x={"format-suffix": {
16+
* "enabled": true
17+
* }}
1518
* )
1619
*/
1720
class Controller
@@ -32,7 +35,9 @@ public function inheritEnabledFormatSuffix(): void
3235
/**
3336
* @SWG\Get(
3437
* path="/b",
35-
* x={"format-pattern": "json|xml"},
38+
* x={"format-suffix": {
39+
* "pattern": "json|xml"
40+
* }},
3641
* @SWG\Response(
3742
* response="200",
3843
* description="Success",

tests/OpenApiRouteLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testBasic(): void
4242
public function testFormatSuffix(): void
4343
{
4444
$finder = (new Finder())->in(__DIR__.'/Fixtures/FormatSuffix');
45-
$routeLoader = new OpenApiRouteLoader($finder, true);
45+
$routeLoader = new OpenApiRouteLoader($finder);
4646

4747
$routes = $routeLoader->__invoke();
4848

0 commit comments

Comments
 (0)