|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Functional; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue8143\ReferenceResponse; |
| 18 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 19 | +use Symfony\Component\Config\Loader\LoaderInterface; |
| 20 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 21 | + |
| 22 | +class OpenApiNameConverterAppKernel extends \AppKernel |
| 23 | +{ |
| 24 | + public function getCacheDir(): string |
| 25 | + { |
| 26 | + return parent::getCacheDir().'/openapi_name_converter'; |
| 27 | + } |
| 28 | + |
| 29 | + public function getLogDir(): string |
| 30 | + { |
| 31 | + return parent::getLogDir().'/openapi_name_converter'; |
| 32 | + } |
| 33 | + |
| 34 | + protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void |
| 35 | + { |
| 36 | + parent::configureContainer($c, $loader); |
| 37 | + |
| 38 | + $loader->load(static function (ContainerBuilder $container): void { |
| 39 | + $container->loadFromExtension('framework', [ |
| 40 | + 'serializer' => [ |
| 41 | + 'name_converter' => 'serializer.name_converter.camel_case_to_snake_case', |
| 42 | + ], |
| 43 | + ]); |
| 44 | + }); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +final class OpenApiNameConverterTest extends ApiTestCase |
| 49 | +{ |
| 50 | + use SetupClassResourcesTrait; |
| 51 | + |
| 52 | + protected static ?bool $alwaysBootKernel = true; |
| 53 | + |
| 54 | + /** |
| 55 | + * @return class-string[] |
| 56 | + */ |
| 57 | + public static function getResources(): array |
| 58 | + { |
| 59 | + return [ReferenceResponse::class]; |
| 60 | + } |
| 61 | + |
| 62 | + protected static function getKernelClass(): string |
| 63 | + { |
| 64 | + return OpenApiNameConverterAppKernel::class; |
| 65 | + } |
| 66 | + |
| 67 | + public function testGlobalNameConverterDoesNotLeakIntoOpenApiDocument(): void |
| 68 | + { |
| 69 | + $response = self::createClient()->request('GET', '/docs', [ |
| 70 | + 'headers' => ['Accept' => 'application/vnd.openapi+json'], |
| 71 | + ]); |
| 72 | + |
| 73 | + $this->assertResponseIsSuccessful(); |
| 74 | + $json = $response->toArray(); |
| 75 | + $content = $response->getContent(); |
| 76 | + |
| 77 | + // OpenAPI keys must stay camelCase even when a global name converter is configured. |
| 78 | + $this->assertStringContainsString('"operationId"', $content); |
| 79 | + $this->assertStringNotContainsString('"operation_id"', $content); |
| 80 | + $this->assertStringNotContainsString('"extension_properties"', $content); |
| 81 | + $this->assertStringNotContainsString('"external_docs"', $content); |
| 82 | + $this->assertStringNotContainsString('"request_bodies"', $content); |
| 83 | + $this->assertStringNotContainsString('"security_schemes"', $content); |
| 84 | + |
| 85 | + // The #[SerializedName('$ref')] metadata must still be honored. |
| 86 | + $responses = $json['paths']['/issue8143_reference_response']['post']['responses']; |
| 87 | + $this->assertArrayHasKey('$ref', $responses['401']); |
| 88 | + $this->assertSame('#/components/responses/401', $responses['401']['$ref']); |
| 89 | + } |
| 90 | +} |
0 commit comments