2016-07-12 00:33:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2016-12-29 12:09:26 +01:00
|
|
|
* This file is part of the NelmioApiDocBundle package.
|
2016-07-12 00:33:55 +02:00
|
|
|
*
|
2016-12-29 12:09:26 +01:00
|
|
|
* (c) Nelmio
|
2016-07-12 00:33:55 +02:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2016-12-29 12:09:26 +01:00
|
|
|
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
2016-07-12 00:33:55 +02:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
|
2021-06-16 10:59:06 +03:00
|
|
|
use Nelmio\ApiDocBundle\Tests\Helper;
|
2020-05-28 13:19:11 +02:00
|
|
|
use OpenApi\Annotations as OA;
|
2021-12-11 16:39:04 +03:00
|
|
|
use OpenApi\Generator;
|
2020-05-30 18:08:25 +02:00
|
|
|
use Symfony\Component\Serializer\Annotation\SerializedName;
|
2016-07-12 00:33:55 +02:00
|
|
|
|
|
|
|
class FunctionalTest extends WebTestCase
|
|
|
|
{
|
2020-05-31 14:16:51 +01:00
|
|
|
protected function setUp(): void
|
2019-12-19 15:41:14 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
static::createClient([], ['HTTP_HOST' => 'api.example.com']);
|
|
|
|
}
|
|
|
|
|
2017-01-25 18:53:19 +01:00
|
|
|
public function testConfiguredDocumentation()
|
|
|
|
{
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertEquals('My Default App', $this->getOpenApiDefinition()->info->title);
|
|
|
|
$this->assertEquals('My Test App', $this->getOpenApiDefinition('test')->info->title);
|
2017-01-25 18:53:19 +01:00
|
|
|
}
|
|
|
|
|
2016-11-30 14:08:10 +01:00
|
|
|
public function testUndocumentedAction()
|
|
|
|
{
|
2020-05-28 13:19:11 +02:00
|
|
|
$api = $this->getOpenApiDefinition();
|
|
|
|
|
|
|
|
$this->assertNotHasPath('/undocumented', $api);
|
|
|
|
$this->assertNotHasPath('/api/admin', $api);
|
2016-11-30 14:08:10 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 17:16:14 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideArticleRoute
|
|
|
|
*/
|
|
|
|
public function testFetchArticleAction(string $articleRoute)
|
2017-06-13 13:34:26 +02:00
|
|
|
{
|
2021-12-21 17:16:14 +02:00
|
|
|
$operation = $this->getOperation($articleRoute, 'get');
|
2017-06-13 13:34:26 +02:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertHasResponse('200', $operation);
|
|
|
|
$response = $this->getOperationResponse($operation, '200');
|
|
|
|
$this->assertEquals('#/components/schemas/Article', $response->content['application/json']->schema->ref);
|
2017-06-13 13:34:26 +02:00
|
|
|
|
|
|
|
// Ensure that groups are supported
|
2020-05-28 13:19:11 +02:00
|
|
|
$articleModel = $this->getModel('Article');
|
|
|
|
$this->assertCount(1, $articleModel->properties);
|
|
|
|
$this->assertHasProperty('author', $articleModel);
|
|
|
|
$this->assertSame('#/components/schemas/User2', Util::getProperty($articleModel, 'author')->ref);
|
|
|
|
$this->assertNotHasProperty('author', Util::getProperty($articleModel, 'author'));
|
2017-06-13 13:34:26 +02:00
|
|
|
}
|
|
|
|
|
2021-12-21 17:16:14 +02:00
|
|
|
public function provideArticleRoute(): iterable
|
|
|
|
{
|
|
|
|
yield 'Annotations' => ['/api/article/{id}'];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80100) {
|
|
|
|
yield 'Attributes' => ['/api/article_attributes/{id}'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 19:35:04 +01:00
|
|
|
public function testFilteredAction()
|
|
|
|
{
|
2020-05-28 13:19:11 +02:00
|
|
|
$openApi = $this->getOpenApiDefinition();
|
2017-03-16 19:35:04 +01:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertNotHasPath('/filtered', $openApi);
|
2017-03-16 19:35:04 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 13:37:02 +01:00
|
|
|
/**
|
|
|
|
* Tests that the paths are automatically resolved in Swagger annotations.
|
|
|
|
*
|
|
|
|
* @dataProvider swaggerActionPathsProvider
|
|
|
|
*/
|
2020-05-28 13:19:11 +02:00
|
|
|
public function testSwaggerAction(string $path)
|
2016-12-30 13:37:02 +01:00
|
|
|
{
|
|
|
|
$operation = $this->getOperation($path, 'get');
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertHasResponse('201', $operation);
|
|
|
|
$response = $this->getOperationResponse($operation, '201');
|
|
|
|
$this->assertEquals('An example resource', $response->description);
|
2016-12-30 13:37:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function swaggerActionPathsProvider()
|
|
|
|
{
|
|
|
|
return [['/api/swagger'], ['/api/swagger2']];
|
|
|
|
}
|
|
|
|
|
2020-12-27 00:53:57 +01:00
|
|
|
public function testAnnotationWithManualPath()
|
|
|
|
{
|
|
|
|
$path = $this->getPath('/api/swagger2');
|
2021-12-11 16:39:04 +03:00
|
|
|
$this->assertSame(Generator::UNDEFINED, $path->post);
|
2020-12-27 00:53:57 +01:00
|
|
|
|
|
|
|
$operation = $this->getOperation('/api/swagger', 'get');
|
|
|
|
$this->assertNotHasParameter('Accept-Version', 'header', $operation);
|
|
|
|
|
|
|
|
$operation = $this->getOperation('/api/swagger2', 'get');
|
|
|
|
$this->assertHasParameter('Accept-Version', 'header', $operation);
|
|
|
|
}
|
|
|
|
|
2016-12-30 13:37:02 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider implicitSwaggerActionMethodsProvider
|
|
|
|
*/
|
2020-05-28 13:19:11 +02:00
|
|
|
public function testImplicitSwaggerAction(string $method)
|
2016-12-30 13:37:02 +01:00
|
|
|
{
|
|
|
|
$operation = $this->getOperation('/api/swagger/implicit', $method);
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertEquals(['implicit'], $operation->tags);
|
2016-12-30 13:37:02 +01:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertHasResponse('201', $operation);
|
|
|
|
$response = $this->getOperationResponse($operation, '201');
|
|
|
|
$this->assertEquals('Operation automatically detected', $response->description);
|
|
|
|
$this->assertEquals('#/components/schemas/User', $response->content['application/json']->schema->ref);
|
2017-01-14 17:36:56 +01:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertInstanceOf(OA\RequestBody::class, $operation->requestBody);
|
|
|
|
$requestBody = $operation->requestBody;
|
|
|
|
$this->assertEquals('This is a request body', $requestBody->description);
|
|
|
|
$this->assertEquals('array', $requestBody->content['application/json']->schema->type);
|
|
|
|
$this->assertEquals('#/components/schemas/User', $requestBody->content['application/json']->schema->items->ref);
|
2016-12-30 13:37:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function implicitSwaggerActionMethodsProvider()
|
|
|
|
{
|
|
|
|
return [['get'], ['post']];
|
|
|
|
}
|
|
|
|
|
2016-07-13 23:05:14 +02:00
|
|
|
public function testUserAction()
|
2016-07-12 00:33:55 +02:00
|
|
|
{
|
2016-11-30 14:08:10 +01:00
|
|
|
$operation = $this->getOperation('/api/test/{user}', 'get');
|
2016-07-12 00:33:55 +02:00
|
|
|
|
2021-12-11 16:39:04 +03:00
|
|
|
$this->assertEquals(Generator::UNDEFINED, $operation->security);
|
|
|
|
$this->assertEquals(Generator::UNDEFINED, $operation->summary);
|
|
|
|
$this->assertEquals(Generator::UNDEFINED, $operation->description);
|
|
|
|
$this->assertEquals(Generator::UNDEFINED, $operation->deprecated);
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertHasResponse(200, $operation);
|
|
|
|
|
|
|
|
$this->assertHasParameter('user', 'path', $operation);
|
|
|
|
$parameter = Util::getOperationParameter($operation, 'user', 'path');
|
|
|
|
$this->assertTrue($parameter->required);
|
|
|
|
$this->assertEquals('string', $parameter->schema->type);
|
|
|
|
$this->assertEquals('/foo/', $parameter->schema->pattern);
|
2021-12-11 16:39:04 +03:00
|
|
|
$this->assertEquals(Generator::UNDEFINED, $parameter->schema->format);
|
2016-07-12 00:33:55 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 23:05:14 +02:00
|
|
|
public function testDeprecatedAction()
|
|
|
|
{
|
2016-11-30 14:08:10 +01:00
|
|
|
$operation = $this->getOperation('/api/deprecated', 'get');
|
2016-07-13 23:05:14 +02:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertEquals('This action is deprecated.', $operation->summary);
|
|
|
|
$this->assertEquals('Please do not use this action.', $operation->description);
|
|
|
|
$this->assertTrue($operation->deprecated);
|
2016-07-13 23:05:14 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 19:58:57 +02:00
|
|
|
public function testApiPlatform()
|
2016-07-28 10:20:59 +02:00
|
|
|
{
|
2016-08-01 19:58:57 +02:00
|
|
|
$operation = $this->getOperation('/api/dummies', 'get');
|
|
|
|
$operation = $this->getOperation('/api/foo', 'get');
|
|
|
|
$operation = $this->getOperation('/api/foo', 'post');
|
|
|
|
$operation = $this->getOperation('/api/dummies/{id}', 'get');
|
2016-07-28 10:20:59 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:56 +01:00
|
|
|
public function testUserModel()
|
|
|
|
{
|
2017-12-03 20:30:44 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => [
|
|
|
|
'money' => [
|
|
|
|
'type' => 'number',
|
|
|
|
'format' => 'float',
|
2017-12-18 21:07:44 +01:00
|
|
|
'default' => 0.0,
|
2017-12-03 20:30:44 +02:00
|
|
|
],
|
|
|
|
'id' => [
|
|
|
|
'type' => 'integer',
|
2017-12-17 10:44:07 +01:00
|
|
|
'description' => 'User id',
|
2017-12-03 20:30:44 +02:00
|
|
|
'readOnly' => true,
|
2017-12-17 10:44:07 +01:00
|
|
|
'title' => 'userid',
|
2017-12-03 20:30:44 +02:00
|
|
|
'example' => 1,
|
2020-05-28 13:19:11 +02:00
|
|
|
'default' => null,
|
2017-12-03 20:30:44 +02:00
|
|
|
],
|
|
|
|
'email' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'readOnly' => false,
|
|
|
|
],
|
2017-12-15 17:39:18 +01:00
|
|
|
'roles' => [
|
2017-12-17 17:58:41 +02:00
|
|
|
'title' => 'roles',
|
2017-12-15 17:39:18 +01:00
|
|
|
'type' => 'array',
|
|
|
|
'description' => 'User roles',
|
|
|
|
'example' => '["ADMIN","SUPERUSER"]',
|
|
|
|
'items' => ['type' => 'string'],
|
2017-12-17 17:58:41 +02:00
|
|
|
'default' => ['user'],
|
2017-12-15 17:39:18 +01:00
|
|
|
],
|
2020-05-31 09:30:13 +01:00
|
|
|
'location' => [
|
|
|
|
'title' => 'User Location.',
|
|
|
|
'type' => 'string',
|
|
|
|
],
|
2017-12-03 20:30:44 +02:00
|
|
|
'friendsNumber' => [
|
|
|
|
'type' => 'string',
|
|
|
|
],
|
2018-02-19 21:41:05 +01:00
|
|
|
'creationDate' => [
|
2017-12-03 20:30:44 +02:00
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'date-time',
|
|
|
|
],
|
|
|
|
'users' => [
|
|
|
|
'items' => [
|
2020-05-28 13:19:11 +02:00
|
|
|
'$ref' => '#/components/schemas/User',
|
2017-12-03 20:30:44 +02:00
|
|
|
],
|
|
|
|
'type' => 'array',
|
|
|
|
],
|
2018-08-30 01:10:36 +02:00
|
|
|
'friend' => [
|
2020-06-16 13:11:53 +02:00
|
|
|
'nullable' => true,
|
|
|
|
'allOf' => [
|
|
|
|
['$ref' => '#/components/schemas/User'],
|
|
|
|
],
|
2018-08-30 01:10:36 +02:00
|
|
|
],
|
2020-08-06 10:22:59 +02:00
|
|
|
'friends' => [
|
|
|
|
'nullable' => true,
|
|
|
|
'items' => [
|
|
|
|
'$ref' => '#/components/schemas/User',
|
|
|
|
],
|
|
|
|
'type' => 'array',
|
|
|
|
],
|
2017-12-03 20:30:44 +02:00
|
|
|
'dummy' => [
|
2020-05-28 13:19:11 +02:00
|
|
|
'$ref' => '#/components/schemas/Dummy2',
|
2017-12-03 20:30:44 +02:00
|
|
|
],
|
2017-12-17 17:58:41 +02:00
|
|
|
'status' => [
|
|
|
|
'type' => 'string',
|
2017-12-18 21:07:44 +01:00
|
|
|
'enum' => ['disabled', 'enabled'],
|
2017-12-17 17:58:41 +02:00
|
|
|
],
|
2018-12-10 14:21:09 +01:00
|
|
|
'dateAsInterface' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'date-time',
|
2018-12-10 14:25:08 +01:00
|
|
|
],
|
2017-12-03 20:30:44 +02:00
|
|
|
],
|
2020-05-28 13:19:11 +02:00
|
|
|
'schema' => 'User',
|
2017-12-03 20:30:44 +02:00
|
|
|
],
|
2020-05-28 13:19:11 +02:00
|
|
|
json_decode($this->getModel('User')->toJson(), true)
|
2017-12-03 20:30:44 +02:00
|
|
|
);
|
2017-01-14 17:36:56 +01:00
|
|
|
}
|
|
|
|
|
2017-06-24 17:49:00 +02:00
|
|
|
public function testFormSupport()
|
|
|
|
{
|
2017-09-20 08:18:58 +03:00
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'object',
|
2020-12-02 15:38:38 +01:00
|
|
|
'description' => 'this is the description of an user',
|
2017-09-20 08:18:58 +03:00
|
|
|
'properties' => [
|
2018-04-27 11:57:21 +02:00
|
|
|
'strings' => [
|
|
|
|
'items' => ['type' => 'string'],
|
|
|
|
'type' => 'array',
|
|
|
|
],
|
2020-05-28 13:19:11 +02:00
|
|
|
'dummy' => ['$ref' => '#/components/schemas/DummyType'],
|
2017-11-11 13:33:41 +02:00
|
|
|
'dummies' => [
|
2020-05-28 13:19:11 +02:00
|
|
|
'items' => ['$ref' => '#/components/schemas/DummyType'],
|
2017-11-11 13:33:41 +02:00
|
|
|
'type' => 'array',
|
2017-12-17 10:44:07 +01:00
|
|
|
],
|
2018-04-27 11:57:21 +02:00
|
|
|
'empty_dummies' => [
|
2020-05-28 13:19:11 +02:00
|
|
|
'items' => ['$ref' => '#/components/schemas/DummyEmptyType'],
|
2018-04-27 11:57:21 +02:00
|
|
|
'type' => 'array',
|
|
|
|
],
|
2018-02-19 10:56:51 +01:00
|
|
|
'quz' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'description' => 'User type.',
|
|
|
|
],
|
2018-05-11 00:21:26 +02:00
|
|
|
'entity' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'Entity id',
|
|
|
|
],
|
2018-08-29 22:14:19 +01:00
|
|
|
'entities' => [
|
|
|
|
'type' => 'array',
|
|
|
|
'format' => '[Entity id]',
|
|
|
|
'items' => ['type' => 'string'],
|
|
|
|
],
|
|
|
|
'document' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'Document id',
|
|
|
|
],
|
|
|
|
'documents' => [
|
|
|
|
'type' => 'array',
|
|
|
|
'format' => '[Document id]',
|
|
|
|
'items' => ['type' => 'string'],
|
|
|
|
],
|
2018-05-09 23:30:21 +02:00
|
|
|
'extended_builtin' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'enum' => ['foo', 'bar'],
|
|
|
|
],
|
2019-03-07 08:48:56 +01:00
|
|
|
'save' => [
|
|
|
|
],
|
2017-09-20 08:18:58 +03:00
|
|
|
],
|
2018-08-29 22:14:19 +01:00
|
|
|
'required' => ['dummy', 'dummies', 'entity', 'entities', 'document', 'documents', 'extended_builtin'],
|
2020-05-28 13:19:11 +02:00
|
|
|
'schema' => 'UserType',
|
|
|
|
], json_decode($this->getModel('UserType')->toJson(), true));
|
2017-09-20 08:18:58 +03:00
|
|
|
|
2017-06-24 17:49:00 +02:00
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => [
|
|
|
|
'bar' => [
|
|
|
|
'type' => 'string',
|
|
|
|
],
|
|
|
|
'foo' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'enum' => ['male', 'female'],
|
2017-06-26 10:34:42 +02:00
|
|
|
],
|
2018-09-24 17:35:57 +02:00
|
|
|
'boo' => [
|
|
|
|
'type' => 'boolean',
|
|
|
|
'enum' => [true, false],
|
|
|
|
],
|
2018-02-03 12:52:43 +01:00
|
|
|
'foz' => [
|
|
|
|
'type' => 'array',
|
|
|
|
'items' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'enum' => ['male', 'female'],
|
|
|
|
],
|
|
|
|
],
|
2017-12-15 16:58:40 +01:00
|
|
|
'baz' => [
|
2017-12-17 10:44:07 +01:00
|
|
|
'type' => 'boolean',
|
|
|
|
],
|
2017-12-19 00:22:26 +01:00
|
|
|
'bey' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
],
|
2018-08-30 00:16:19 +02:00
|
|
|
'password' => [
|
|
|
|
'type' => 'object',
|
|
|
|
'required' => ['first_field', 'second'],
|
|
|
|
'properties' => [
|
|
|
|
'first_field' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'password',
|
|
|
|
],
|
|
|
|
'second' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'format' => 'password',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2017-06-24 17:49:00 +02:00
|
|
|
],
|
2018-08-30 00:16:19 +02:00
|
|
|
'required' => ['foo', 'foz', 'password'],
|
2020-05-28 13:19:11 +02:00
|
|
|
'schema' => 'DummyType',
|
|
|
|
], json_decode($this->getModel('DummyType')->toJson(), true));
|
2022-06-10 22:41:24 +02:00
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => [
|
|
|
|
'quz' => [
|
|
|
|
'$ref' => '#/components/schemas/User',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'required' => ['quz'],
|
|
|
|
'schema' => 'FormWithModel',
|
|
|
|
], json_decode($this->getModel('FormWithModel')->toJson(), true));
|
2017-06-24 17:49:00 +02:00
|
|
|
}
|
2018-01-25 21:11:34 +01:00
|
|
|
|
2021-12-21 16:39:08 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideSecurityRoute
|
|
|
|
*/
|
|
|
|
public function testSecurityAction(string $route)
|
2018-01-25 21:11:34 +01:00
|
|
|
{
|
2021-12-21 16:39:08 +01:00
|
|
|
$operation = $this->getOperation($route, 'get');
|
2018-01-25 21:11:34 +01:00
|
|
|
|
|
|
|
$expected = [
|
|
|
|
['api_key' => []],
|
|
|
|
['basic' => []],
|
2021-01-26 19:05:27 +01:00
|
|
|
['oauth2' => ['scope_1']],
|
2018-01-25 21:11:34 +01:00
|
|
|
];
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertEquals($expected, $operation->security);
|
2018-01-25 21:11:34 +01:00
|
|
|
}
|
2018-02-05 18:39:58 +01:00
|
|
|
|
2021-12-21 16:39:08 +01:00
|
|
|
public function provideSecurityRoute(): iterable
|
|
|
|
{
|
|
|
|
yield 'Annotations' => ['/api/security'];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80100) {
|
|
|
|
yield 'Attributes' => ['/api/security_attributes'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:33:35 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideSecurityOverrideRoute
|
|
|
|
*/
|
|
|
|
public function testSecurityOverrideAction(string $route)
|
|
|
|
{
|
|
|
|
$operation = $this->getOperation($route, 'get');
|
|
|
|
$this->assertEquals([], $operation->security);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideSecurityOverrideRoute(): iterable
|
|
|
|
{
|
|
|
|
yield 'Annotations' => ['/api/securityOverride'];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80100) {
|
|
|
|
yield 'Attributes' => ['/api/security_override_attributes'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 14:37:14 +02:00
|
|
|
public function testInlinePHP81Parameters()
|
|
|
|
{
|
|
|
|
if (\PHP_VERSION_ID < 80100) {
|
|
|
|
$this->markTestSkipped('Attributes require PHP 8.1');
|
|
|
|
}
|
|
|
|
|
|
|
|
$operation = $this->getOperation('/api/inline_path_parameters', 'get');
|
|
|
|
$this->assertCount(1, $operation->parameters);
|
|
|
|
$this->assertInstanceOf(OA\PathParameter::class, $operation->parameters[0]);
|
|
|
|
$this->assertSame($operation->parameters[0]->name, 'product_id');
|
|
|
|
$this->assertSame($operation->parameters[0]->schema->type, 'string');
|
|
|
|
}
|
|
|
|
|
2018-04-16 13:43:42 +02:00
|
|
|
public function testClassSecurityAction()
|
|
|
|
{
|
|
|
|
$operation = $this->getOperation('/api/security/class', 'get');
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
['basic' => []],
|
|
|
|
];
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertEquals($expected, $operation->security);
|
2018-04-16 13:43:42 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 18:39:58 +01:00
|
|
|
public function testSymfonyConstraintDocumentation()
|
|
|
|
{
|
2021-06-16 10:59:06 +03:00
|
|
|
$expected = [
|
2018-02-05 18:39:58 +01:00
|
|
|
'required' => [
|
|
|
|
'propertyNotBlank',
|
|
|
|
'propertyNotNull',
|
|
|
|
],
|
|
|
|
'properties' => [
|
|
|
|
'propertyNotBlank' => [
|
|
|
|
'type' => 'integer',
|
2020-07-12 14:54:39 +02:00
|
|
|
'maxItems' => '10',
|
|
|
|
'minItems' => '0',
|
2018-02-05 18:39:58 +01:00
|
|
|
],
|
|
|
|
'propertyNotNull' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
],
|
2018-08-30 00:32:11 +02:00
|
|
|
'propertyAssertLength' => [
|
2018-02-05 18:39:58 +01:00
|
|
|
'type' => 'integer',
|
|
|
|
'maxLength' => '50',
|
|
|
|
'minLength' => '0',
|
|
|
|
],
|
|
|
|
'propertyRegex' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
'pattern' => '.*[a-z]{2}.*',
|
|
|
|
],
|
|
|
|
'propertyCount' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
'maxItems' => '10',
|
|
|
|
'minItems' => '0',
|
|
|
|
],
|
|
|
|
'propertyChoice' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
'enum' => ['choice1', 'choice2'],
|
|
|
|
],
|
2018-05-20 15:59:52 +02:00
|
|
|
'propertyChoiceWithCallback' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
'enum' => ['choice1', 'choice2'],
|
|
|
|
],
|
2018-09-26 16:51:43 +02:00
|
|
|
'propertyChoiceWithCallbackWithoutClass' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
'enum' => ['choice1', 'choice2'],
|
|
|
|
],
|
Apply `enum` from Choice Constraints to Items When Choice is Multiple (#1784)
* Apply `enum` from Choice Constraints to Items When Choice is Multiple
Otherwise JSON schema like this is generated:
```
"property": {
"type": "array",
"enum": ["one", "two", "three"],
"items": {
"type": "string"
}
}
```
With this change, however, this schema is generated:
```
"property": {
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
```
A possible downside here is that the symfony constraint stuff happens
before types are figured out from PHPDoc. So it's _possible_ to end up
with something that won't validated. Take something like this:
```
/**
* @Assert\Choice(multiple=true, choices={"..."})
* @var string
*/
```
This would generate:
```
"property": {
"type": "string",
"items": {
"enum": ["..."]
}
}
```
* Fix CS
* cs
* more cs
* fix tests
Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
2021-02-19 02:41:32 -06:00
|
|
|
'propertyChoiceWithMultiple' => [
|
|
|
|
'type' => 'array',
|
|
|
|
'items' => [
|
|
|
|
'type' => 'string',
|
|
|
|
'enum' => ['choice1', 'choice2'],
|
|
|
|
],
|
|
|
|
],
|
2018-02-05 18:39:58 +01:00
|
|
|
'propertyExpression' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
],
|
2018-12-19 15:41:27 +01:00
|
|
|
'propertyRange' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
'maximum' => 5,
|
|
|
|
'minimum' => 1,
|
|
|
|
],
|
|
|
|
'propertyLessThan' => [
|
|
|
|
'type' => 'integer',
|
2020-07-06 19:50:34 +02:00
|
|
|
'exclusiveMaximum' => true,
|
|
|
|
'maximum' => 42,
|
2018-12-19 15:41:27 +01:00
|
|
|
],
|
|
|
|
'propertyLessThanOrEqual' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
'maximum' => 23,
|
|
|
|
],
|
2021-06-16 10:59:06 +03:00
|
|
|
'propertyWithCompoundValidationRule' => [
|
|
|
|
'type' => 'integer',
|
|
|
|
],
|
2018-02-05 18:39:58 +01:00
|
|
|
],
|
|
|
|
'type' => 'object',
|
2020-05-28 13:19:11 +02:00
|
|
|
'schema' => 'SymfonyConstraints',
|
2021-06-16 10:59:06 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
if (Helper::isCompoundValidatorConstraintSupported()) {
|
|
|
|
$expected['required'][] = 'propertyWithCompoundValidationRule';
|
|
|
|
$expected['properties']['propertyWithCompoundValidationRule'] = [
|
|
|
|
'type' => 'integer',
|
|
|
|
'maximum' => 5,
|
|
|
|
'exclusiveMaximum' => true,
|
|
|
|
'minimum' => 0,
|
|
|
|
'exclusiveMinimum' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($expected, json_decode($this->getModel('SymfonyConstraints')->toJson(), true));
|
2018-02-05 18:39:58 +01:00
|
|
|
}
|
2018-02-19 10:49:52 +01:00
|
|
|
|
|
|
|
public function testConfigReference()
|
|
|
|
{
|
|
|
|
$operation = $this->getOperation('/api/configReference', 'get');
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertEquals('#/components/schemas/Test', $this->getOperationResponse($operation, '200')->ref);
|
|
|
|
$this->assertEquals('#/components/responses/201', $this->getOperationResponse($operation, '201')->ref);
|
2018-02-19 10:49:52 +01:00
|
|
|
}
|
2018-06-02 13:48:44 +02:00
|
|
|
|
|
|
|
public function testOperationsWithOtherAnnotationsAction()
|
|
|
|
{
|
|
|
|
$getOperation = $this->getOperation('/api/multi-annotations', 'get');
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertSame('This is the get operation', $getOperation->description);
|
|
|
|
$this->assertSame('Worked well!', $this->getOperationResponse($getOperation, 200)->description);
|
2018-06-02 13:48:44 +02:00
|
|
|
|
|
|
|
$postOperation = $this->getOperation('/api/multi-annotations', 'post');
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertSame('This is post', $postOperation->description);
|
|
|
|
$this->assertSame('Worked well!', $this->getOperationResponse($postOperation, 200)->description);
|
2018-06-02 13:48:44 +02:00
|
|
|
}
|
2019-04-10 20:52:45 +02:00
|
|
|
|
|
|
|
public function testNoDuplicatedParameters()
|
|
|
|
{
|
2020-11-01 11:41:49 +01:00
|
|
|
$this->assertHasPath('/api/article/{id}', $this->getOpenApiDefinition());
|
|
|
|
$this->assertNotHasParameter('id', 'path', $this->getOperation('/api/article/{id}', 'get'));
|
2019-04-10 20:52:45 +02:00
|
|
|
}
|
2020-05-30 18:08:25 +02:00
|
|
|
|
|
|
|
public function testSerializedNameAction()
|
|
|
|
{
|
2020-08-06 10:26:59 +02:00
|
|
|
if (!class_exists(SerializedName::class)) {
|
2020-05-30 18:08:25 +02:00
|
|
|
$this->markTestSkipped('Annotation @SerializedName doesn\'t exist.');
|
|
|
|
}
|
|
|
|
|
2020-05-30 18:23:49 +02:00
|
|
|
$model = $this->getModel('SerializedNameEnt');
|
|
|
|
$this->assertCount(2, $model->properties);
|
2020-05-30 18:08:25 +02:00
|
|
|
|
2020-05-30 18:23:49 +02:00
|
|
|
$this->assertNotHasProperty('foo', $model);
|
|
|
|
$this->assertHasProperty('notfoo', $model);
|
2020-05-30 18:08:25 +02:00
|
|
|
|
2020-05-30 18:23:49 +02:00
|
|
|
$this->assertNotHasProperty('bar', $model);
|
|
|
|
$this->assertHasProperty('notwhatyouthink', $model);
|
2020-05-30 18:08:25 +02:00
|
|
|
}
|
2020-06-16 13:11:53 +02:00
|
|
|
|
|
|
|
public function testCompoundEntityAction()
|
|
|
|
{
|
|
|
|
$model = $this->getModel('CompoundEntity');
|
|
|
|
$this->assertCount(1, $model->properties);
|
|
|
|
|
|
|
|
$this->assertHasProperty('complex', $model);
|
|
|
|
|
|
|
|
$property = $model->properties[0];
|
|
|
|
$this->assertCount(2, $property->oneOf);
|
|
|
|
|
|
|
|
$this->assertSame('integer', $property->oneOf[0]->type);
|
|
|
|
$this->assertSame('array', $property->oneOf[1]->type);
|
|
|
|
$this->assertSame('#/components/schemas/CompoundEntity', $property->oneOf[1]->items->ref);
|
|
|
|
}
|
2020-07-11 17:53:09 +02:00
|
|
|
|
|
|
|
public function testInvokableController()
|
|
|
|
{
|
|
|
|
$operation = $this->getOperation('/api/invoke', 'get');
|
2020-07-11 18:06:00 +02:00
|
|
|
$this->assertSame('Invokable!', $this->getOperationResponse($operation, 200)->description);
|
2020-07-11 17:53:09 +02:00
|
|
|
}
|
2020-07-24 08:37:58 +02:00
|
|
|
|
|
|
|
public function testDefaultOperationId()
|
|
|
|
{
|
|
|
|
$operation = $this->getOperation('/api/article/{id}', 'get');
|
2021-11-22 22:18:16 +03:00
|
|
|
$this->assertEquals('get_api_nelmio_apidoc_tests_functional_api_fetcharticle', $operation->operationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNamedRouteOperationId()
|
|
|
|
{
|
|
|
|
$operation = $this->getOperation('/api/named_route-operation-id', 'get');
|
|
|
|
$this->assertEquals('get_api_named_route_operation_id', $operation->operationId);
|
|
|
|
|
|
|
|
$operation = $this->getOperation('/api/named_route-operation-id', 'post');
|
|
|
|
$this->assertEquals('post_api_named_route_operation_id', $operation->operationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCustomOperationId()
|
|
|
|
{
|
|
|
|
$operation = $this->getOperation('/api/custom-operation-id', 'get');
|
2022-03-21 17:03:16 +01:00
|
|
|
$this->assertEquals('get-custom-operation-id', $operation->operationId);
|
2021-11-22 22:18:16 +03:00
|
|
|
|
|
|
|
$operation = $this->getOperation('/api/custom-operation-id', 'post');
|
2022-03-21 17:03:16 +01:00
|
|
|
$this->assertEquals('post-custom-operation-id', $operation->operationId);
|
2020-07-24 08:37:58 +02:00
|
|
|
}
|
2020-12-16 23:44:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Related to https://github.com/nelmio/NelmioApiDocBundle/issues/1756
|
|
|
|
* Ensures private/protected properties are not exposed, just like the symfony serializer does.
|
|
|
|
*/
|
|
|
|
public function testPrivateProtectedExposure()
|
|
|
|
{
|
|
|
|
// Ensure that groups are supported
|
2020-12-17 00:06:17 +01:00
|
|
|
$model = $this->getModel('PrivateProtectedExposure');
|
|
|
|
$this->assertCount(1, $model->properties);
|
|
|
|
$this->assertHasProperty('publicField', $model);
|
|
|
|
$this->assertNotHasProperty('privateField', $model);
|
|
|
|
$this->assertNotHasProperty('protectedField', $model);
|
|
|
|
$this->assertNotHasProperty('protected', $model);
|
2020-12-16 23:44:26 +01:00
|
|
|
}
|
2021-02-01 08:56:31 -06:00
|
|
|
|
|
|
|
public function testModelsWithDiscriminatorMapAreLoadedWithOpenApiPolymorphism()
|
|
|
|
{
|
|
|
|
$model = $this->getModel('SymfonyDiscriminator');
|
|
|
|
|
|
|
|
$this->assertInstanceOf(OA\Discriminator::class, $model->discriminator);
|
|
|
|
$this->assertSame('type', $model->discriminator->propertyName);
|
|
|
|
$this->assertCount(2, $model->discriminator->mapping);
|
|
|
|
$this->assertArrayHasKey('one', $model->discriminator->mapping);
|
|
|
|
$this->assertArrayHasKey('two', $model->discriminator->mapping);
|
2021-12-11 16:39:04 +03:00
|
|
|
$this->assertNotSame(Generator::UNDEFINED, $model->oneOf);
|
2021-02-01 08:56:31 -06:00
|
|
|
$this->assertCount(2, $model->oneOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDiscriminatorMapLoadsChildrenModels()
|
|
|
|
{
|
|
|
|
// get model does its own assertions
|
|
|
|
$this->getModel('SymfonyDiscriminatorOne');
|
|
|
|
$this->getModel('SymfonyDiscriminatorTwo');
|
|
|
|
}
|
2021-06-07 18:20:25 +02:00
|
|
|
|
|
|
|
public function testNoAdditionalPropertiesSupport()
|
|
|
|
{
|
|
|
|
$model = $this->getModel('AddProp');
|
|
|
|
|
|
|
|
$this->assertFalse($model->additionalProperties);
|
|
|
|
}
|
2022-04-04 11:42:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @requires PHP >= 8.1
|
|
|
|
*/
|
|
|
|
public function testEnumSupport()
|
|
|
|
{
|
|
|
|
$model = $this->getModel('ArticleType81');
|
|
|
|
|
|
|
|
$this->assertSame('string', $model->type);
|
|
|
|
$this->assertCount(2, $model->enum);
|
|
|
|
}
|
2022-04-30 13:28:05 -05:00
|
|
|
|
|
|
|
public function testEntitiesWithOverriddenSchemaTypeDoNotReadOtherProperties()
|
|
|
|
{
|
|
|
|
$model = $this->getModel('EntityWithAlternateType');
|
|
|
|
|
|
|
|
$this->assertSame('array', $model->type);
|
|
|
|
$this->assertSame('string', $model->items->type);
|
|
|
|
$this->assertSame(Generator::UNDEFINED, $model->properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEntitiesWithRefInSchemaDoNoReadOtherProperties()
|
|
|
|
{
|
|
|
|
$model = $this->getModel('EntityWithRef');
|
|
|
|
|
|
|
|
$this->assertSame(Generator::UNDEFINED, $model->type);
|
|
|
|
$this->assertSame('#/components/schemas/Test', $model->ref);
|
|
|
|
$this->assertSame(Generator::UNDEFINED, $model->properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEntitiesWithObjectTypeStillReadProperties()
|
|
|
|
{
|
|
|
|
$model = $this->getModel('EntityWithObjectType');
|
|
|
|
|
|
|
|
$this->assertSame('object', $model->type);
|
|
|
|
$this->assertCount(1, $model->properties);
|
|
|
|
$property = Util::getProperty($model, 'notIgnored');
|
|
|
|
$this->assertSame('string', $property->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormsWithOverriddenSchemaTypeDoNotReadOtherProperties()
|
|
|
|
{
|
|
|
|
$model = $this->getModel('FormWithAlternateSchemaType');
|
|
|
|
|
|
|
|
$this->assertSame('string', $model->type);
|
|
|
|
$this->assertSame(Generator::UNDEFINED, $model->properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormWithRefInSchemaDoNoReadOtherProperties()
|
|
|
|
{
|
|
|
|
$model = $this->getModel('FormWithRefType');
|
|
|
|
|
|
|
|
$this->assertSame(Generator::UNDEFINED, $model->type);
|
|
|
|
$this->assertSame('#/components/schemas/Test', $model->ref);
|
|
|
|
$this->assertSame(Generator::UNDEFINED, $model->properties);
|
|
|
|
}
|
2016-07-12 00:33:55 +02:00
|
|
|
}
|