Skip to content

Commit 4cf03bc

Browse files
committed
test OpenApi
1 parent 4e60ee0 commit 4cf03bc

3 files changed

Lines changed: 99 additions & 1 deletion

File tree

tests/Fixtures/TestBundle/Entity/Book.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515

1616
use ApiPlatform\Metadata\ApiResource;
1717
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
1819
use Doctrine\ORM\Mapping as ORM;
1920

2021
/**
2122
* Book.
2223
*
2324
* @author Antoine Bluchet <soyuka@gmail.com>
2425
*/
25-
#[ApiResource(operations: [new Get(), new Get(uriTemplate: '/books/by_isbn/{isbn}{._format}', requirements: ['isbn' => '.+'], uriVariables: 'isbn')])]
26+
#[ApiResource(operations: [new Get(), new Get(uriTemplate: '/books/by_isbn/{isbn}{._format}', requirements: ['isbn' => '.+'], uriVariables: 'isbn'), new GetCollection()])]
2627
#[ORM\Entity]
2728
class Book
2829
{

tests/Fixtures/app/config/config_common.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ api_platform:
3838
Made with love
3939
enable_swagger: true
4040
enable_swagger_ui: true
41+
defaults:
42+
parameters:
43+
'ApiPlatform\Metadata\HeaderParameter':
44+
key: 'X-Request-ID'
45+
description: 'A unique request identifier'
4146
formats:
4247
jsonld: ['application/ld+json']
4348
jsonhal: ['application/hal+json']
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)