|
| 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\OpenApi; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Book; |
| 18 | +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy; |
| 19 | +use ApiPlatform\Tests\RecreateSchemaTrait; |
| 20 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 21 | + |
| 22 | +/** |
| 23 | + * Test that global default parameters are applied to ALL resources in OpenAPI spec. |
| 24 | + * |
| 25 | + * When parameters are defined in api_platform.defaults.parameters with class names as keys, |
| 26 | + * they should appear in the OpenAPI documentation for EVERY resource, not just specific ones. |
| 27 | + */ |
| 28 | +class GlobalDefaultsParametersOpenApiTest extends ApiTestCase |
| 29 | +{ |
| 30 | + use RecreateSchemaTrait; |
| 31 | + use SetupClassResourcesTrait; |
| 32 | + |
| 33 | + protected static ?bool $alwaysBootKernel = false; |
| 34 | + |
| 35 | + /** |
| 36 | + * @return class-string[] |
| 37 | + */ |
| 38 | + public static function getResources(): array |
| 39 | + { |
| 40 | + return [ |
| 41 | + Book::class, |
| 42 | + Dummy::class, |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Test that global HeaderParameter with description appears in schema. |
| 48 | + * |
| 49 | + * If we configured global parameters like: |
| 50 | + * ```yaml |
| 51 | + * defaults: |
| 52 | + * parameters: |
| 53 | + * 'ApiPlatform\Metadata\HeaderParameter': |
| 54 | + * description: 'A unique request identifier' |
| 55 | + * ``` |
| 56 | + * |
| 57 | + * Then this parameter should appear in OpenAPI for all resources. |
| 58 | + */ |
| 59 | + public function testGlobalHeaderParameterAppearsInSchema(): void |
| 60 | + { |
| 61 | + $globalHeaderParameterDescription = 'A unique request identifier'; |
| 62 | + $globalHeaderParameterKey = 'X-Request-ID'; |
| 63 | + |
| 64 | + $bookResponse = self::createClient()->request('GET', '/docs', [ |
| 65 | + 'headers' => ['Accept' => 'application/vnd.openapi+json'], |
| 66 | + ]); |
| 67 | + |
| 68 | + $this->assertResponseIsSuccessful(); |
| 69 | + $bookRes = $bookResponse->toArray(); |
| 70 | + |
| 71 | + $bookParameters = $bookRes['paths']['/books']['get']['parameters']; |
| 72 | + $this->assertTrue(isset($bookParameters)); |
| 73 | + |
| 74 | + $bookParametersHeader = $bookParameters[4]; |
| 75 | + $this->assertSame($globalHeaderParameterDescription, $bookParametersHeader['description']); |
| 76 | + $this->assertSame($globalHeaderParameterKey, $bookParametersHeader['name']); |
| 77 | + |
| 78 | + $dummyResponse = self::createClient()->request('GET', '/docs', [ |
| 79 | + 'headers' => ['Accept' => 'application/vnd.openapi+json'], |
| 80 | + ]); |
| 81 | + |
| 82 | + $this->assertResponseIsSuccessful(); |
| 83 | + $dummyRes = $dummyResponse->toArray(); |
| 84 | + |
| 85 | + $dummyParameters = $dummyRes['paths']['/dummies/{id}']['get']['parameters']; |
| 86 | + $this->assertTrue(isset($dummyParameters)); |
| 87 | + |
| 88 | + $dummyParametersHeader = $dummyParameters[1]; |
| 89 | + $this->assertSame($globalHeaderParameterDescription, $dummyParametersHeader['description']); |
| 90 | + $this->assertSame($globalHeaderParameterKey, $dummyParametersHeader['name']); |
| 91 | + } |
| 92 | +} |
0 commit comments