Skip to content

Commit 4c87a97

Browse files
authored
fix(openapi): deprecate api_keys names not compatible with 3.1 (#5490)
1 parent 2be8b4f commit 4c87a97

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

features/openapi/docs.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Feature: Documentation support
2929
}
3030
}
3131
},
32-
"Some Authorization Name": {
32+
"Some_Authorization_Name": {
3333
"type": "apiKey",
3434
"description": "Value for the Authorization header parameter.",
3535
"name": "Authorization",

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ private function registerOAuthConfiguration(ContainerBuilder $container, array $
390390
*/
391391
private function registerSwaggerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
392392
{
393+
foreach (array_keys($config['swagger']['api_keys']) as $keyName) {
394+
if (!preg_match('/^[a-zA-Z0-9._-]+$/', $keyName)) {
395+
trigger_deprecation('api-platform/core', '3.1', sprintf('The swagger api_keys key "%s" is not valid with OpenAPI 3.1 it should match "^[a-zA-Z0-9._-]+$"', $keyName));
396+
}
397+
}
398+
393399
$container->setParameter('api_platform.swagger.versions', $config['swagger']['versions']);
394400

395401
if (!$config['enable_swagger'] && $config['enable_swagger_ui']) {

tests/Fixtures/app/config/config_swagger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Config\ApiPlatformConfig;
1717

1818
return static function (ApiPlatformConfig $apiPlatformConfig): void {
19-
$apiPlatformConfig->swagger()->apiKeys('Some Authorization Name')
19+
$apiPlatformConfig->swagger()->apiKeys('Some_Authorization_Name')
2020
->name('Authorization')
2121
->type('header');
2222
};

tests/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,4 +1247,16 @@ public function testHttpCacheBanConfiguration(): void
12471247
$this->assertCount(1, $service->getArguments());
12481248
$this->assertEquals('api_platform.http_cache.http_client', $service->getArgument(0)->getTag());
12491249
}
1250+
1251+
/**
1252+
* @group legacy
1253+
*/
1254+
public function testLegacyOpenApiApiKeysConfiguration(): void
1255+
{
1256+
$this->expectDeprecation('Since api-platform/core 3.1: The swagger api_keys key "Some Authorization Name" is not valid with OpenAPI 3.1 it should match "^[a-zA-Z0-9._-]+$"');
1257+
$config = self::DEFAULT_CONFIG;
1258+
$config['api_platform']['swagger']['api_keys']['Some Authorization Name'] = ['name' => 'a', 'type' => 'header'];
1259+
1260+
(new ApiPlatformExtension())->load($config, $this->container);
1261+
}
12501262
}

0 commit comments

Comments
 (0)