Skip to content

Commit ccef472

Browse files
authored
fix(openapi): use 3.1 version (#5489)
1 parent 8e0d936 commit ccef472

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

features/openapi/docs.feature

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Feature: Documentation support
1010
And the response should be in JSON
1111
And the header "Content-Type" should be equal to "application/json; charset=utf-8"
1212
# Context
13-
And the JSON node "openapi" should be equal to "3.0.0"
13+
And the JSON node "openapi" should be equal to "3.1.0"
1414
# Root properties
1515
And the JSON node "info.title" should be equal to "My Dummy API"
1616
And the JSON node "info.description" should contain "This is a test API."
@@ -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/Metadata/Resource/Factory/LinkFactory.php

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace ApiPlatform\Metadata\Resource\Factory;
1515

1616
use ApiPlatform\Doctrine\Orm\State\Options;
17-
use ApiPlatform\Metadata\ApiResource;
1817
use ApiPlatform\Metadata\Exception\RuntimeException;
1918
use ApiPlatform\Metadata\Link;
2019
use ApiPlatform\Metadata\Metadata;

src/OpenApi/OpenApi.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ final class OpenApi
2222
{
2323
use ExtensionTrait;
2424

25-
// We're actually supporting 3.1 but swagger ui has a version constraint
26-
// public const VERSION = '3.1.0';
27-
public const VERSION = '3.0.0';
25+
public const VERSION = '3.1.0';
2826

2927
private string $openapi = self::VERSION;
3028

src/Symfony/Bundle/DependencyInjection/Configuration.php

+4
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
280280
->end()
281281
->arrayNode('api_keys')
282282
->useAttributeAsKey('key')
283+
->validate()
284+
->ifTrue(static fn($v): bool => (bool) array_filter(array_keys($v), fn($item) => !preg_match('/^[a-zA-Z0-9._-]+$/', $item)))
285+
->thenInvalid('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.')
286+
->end()
283287
->prototype('array')
284288
->children()
285289
->scalarNode('name')

tests/Fixtures/app/config/config_swagger.php

+1-1
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/ConfigurationTest.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,26 @@ public function testExceptionToStatusConfigWithInvalidHttpStatusCodeValue($inval
276276
]);
277277
}
278278

279+
/**
280+
* Test config for api keys.
281+
*/
282+
public function testInvalidApiKeysConfig(): void
283+
{
284+
$this->expectExceptionMessage('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.');
285+
$exampleConfig = [
286+
'name' => 'Authorization',
287+
'type' => 'query',
288+
];
289+
290+
$config = $this->processor->processConfiguration($this->configuration, [
291+
'api_platform' => [
292+
'swagger' => [
293+
'api_keys' => ['Some Authorization name, like JWT' => $exampleConfig, 'Another-Auth' => $exampleConfig],
294+
],
295+
],
296+
]);
297+
}
298+
279299
/**
280300
* Test config for api keys.
281301
*/
@@ -289,13 +309,13 @@ public function testApiKeysConfig(): void
289309
$config = $this->processor->processConfiguration($this->configuration, [
290310
'api_platform' => [
291311
'swagger' => [
292-
'api_keys' => ['Some Authorization name, like JWT' => $exampleConfig],
312+
'api_keys' => ['authorization_name_like_JWT' => $exampleConfig],
293313
],
294314
],
295315
]);
296316

297317
$this->assertArrayHasKey('api_keys', $config['swagger']);
298-
$this->assertSame($exampleConfig, $config['swagger']['api_keys']['Some Authorization name, like JWT']);
318+
$this->assertSame($exampleConfig, $config['swagger']['api_keys']['authorization_name_like_JWT']);
299319
}
300320

301321
/**

0 commit comments

Comments
 (0)