• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 17496868802

05 Sep 2025 03:02PM UTC coverage: 22.604% (+0.05%) from 22.559%
17496868802

push

github

soyuka
fix(symfony): remove deprecation about jsonopenapi

12072 of 53406 relevant lines covered (22.6%)

26.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.4
/src/Symfony/Bundle/DependencyInjection/Configuration.php
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\Symfony\Bundle\DependencyInjection;
15

16
use ApiPlatform\Doctrine\Common\Filter\OrderFilterInterface;
17
use ApiPlatform\Metadata\ApiResource;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\Post;
20
use ApiPlatform\Metadata\Put;
21
use ApiPlatform\Symfony\Controller\MainController;
22
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
23
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
24
use Doctrine\ORM\EntityManagerInterface;
25
use Doctrine\ORM\OptimisticLockException;
26
use GraphQL\GraphQL;
27
use Symfony\Bundle\FrameworkBundle\Controller\ControllerHelper;
28
use Symfony\Bundle\FullStack;
29
use Symfony\Bundle\MakerBundle\MakerBundle;
30
use Symfony\Bundle\MercureBundle\MercureBundle;
31
use Symfony\Bundle\TwigBundle\TwigBundle;
32
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
33
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
34
use Symfony\Component\Config\Definition\ConfigurationInterface;
35
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
36
use Symfony\Component\HttpFoundation\Response;
37
use Symfony\Component\JsonStreamer\JsonStreamWriter;
38
use Symfony\Component\Messenger\MessageBusInterface;
39
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
40
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
41
use Symfony\Component\Yaml\Yaml;
42

43
/**
44
 * The configuration of the bundle.
45
 *
46
 * @author Kévin Dunglas <dunglas@gmail.com>
47
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
48
 */
49
final class Configuration implements ConfigurationInterface
50
{
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getConfigTreeBuilder(): TreeBuilder
55
    {
56
        $treeBuilder = new TreeBuilder('api_platform');
44✔
57
        $rootNode = $treeBuilder->getRootNode();
44✔
58

59
        $rootNode
44✔
60
            ->beforeNormalization()
44✔
61
                ->ifTrue(static function ($v) {
44✔
62
                    return false === ($v['enable_swagger'] ?? null);
44✔
63
                })
44✔
64
                ->then(static function ($v) {
44✔
65
                    $v['swagger']['versions'] = [];
2✔
66

67
                    return $v;
2✔
68
                })
44✔
69
            ->end()
44✔
70
            ->children()
44✔
71
                ->scalarNode('title')
44✔
72
                    ->info('The title of the API.')
44✔
73
                    ->cannotBeEmpty()
44✔
74
                    ->defaultValue('')
44✔
75
                ->end()
44✔
76
                ->scalarNode('description')
44✔
77
                    ->info('The description of the API.')
44✔
78
                    ->cannotBeEmpty()
44✔
79
                    ->defaultValue('')
44✔
80
                ->end()
44✔
81
                ->scalarNode('version')
44✔
82
                    ->info('The version of the API.')
44✔
83
                    ->cannotBeEmpty()
44✔
84
                    ->defaultValue('0.0.0')
44✔
85
                ->end()
44✔
86
                ->booleanNode('show_webby')->defaultTrue()->info('If true, show Webby on the documentation page')->end()
44✔
87
                ->booleanNode('use_symfony_listeners')->defaultFalse()->info(sprintf('Uses Symfony event listeners instead of the %s.', MainController::class))->end()
44✔
88
                ->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()
44✔
89
                ->scalarNode('asset_package')->defaultNull()->info('Specify an asset package name to use.')->end()
44✔
90
                ->scalarNode('path_segment_name_generator')->defaultValue('api_platform.metadata.path_segment_name_generator.underscore')->info('Specify a path name generator to use.')->end()
44✔
91
                ->scalarNode('inflector')->defaultValue('api_platform.metadata.inflector')->info('Specify an inflector to use.')->end()
44✔
92
                ->arrayNode('validator')
44✔
93
                    ->addDefaultsIfNotSet()
44✔
94
                    ->children()
44✔
95
                        ->variableNode('serialize_payload_fields')->defaultValue([])->info('Set to null to serialize all payload fields when a validation error is thrown, or set the fields you want to include explicitly.')->end()
44✔
96
                        ->booleanNode('query_parameter_validation')
44✔
97
                            ->defaultValue(true)
44✔
98
                            ->setDeprecated('api-platform/symfony', '4.2', 'Will be removed in API Platform 5.0.')
44✔
99
                        ->end()
44✔
100
                    ->end()
44✔
101
                ->end()
44✔
102
                ->arrayNode('eager_loading')
44✔
103
                    ->canBeDisabled()
44✔
104
                    ->addDefaultsIfNotSet()
44✔
105
                    ->children()
44✔
106
                        ->booleanNode('fetch_partial')->defaultFalse()->info('Fetch only partial data according to serialization groups. If enabled, Doctrine ORM entities will not work as expected if any of the other fields are used.')->end()
44✔
107
                        ->integerNode('max_joins')->defaultValue(30)->info('Max number of joined relations before EagerLoading throws a RuntimeException')->end()
44✔
108
                        ->booleanNode('force_eager')->defaultTrue()->info('Force join on every relation. If disabled, it will only join relations having the EAGER fetch mode.')->end()
44✔
109
                    ->end()
44✔
110
                ->end()
44✔
111
                ->booleanNode('handle_symfony_errors')->defaultFalse()->info('Allows to handle symfony exceptions.')->end()
44✔
112
                ->booleanNode('enable_swagger')->defaultTrue()->info('Enable the Swagger documentation and export.')->end()
44✔
113
                ->booleanNode('enable_json_streamer')->defaultValue(class_exists(ControllerHelper::class) && class_exists(JsonStreamWriter::class))->info('Enable json streamer.')->end()
44✔
114
                ->booleanNode('enable_swagger_ui')->defaultValue(class_exists(TwigBundle::class))->info('Enable Swagger UI')->end()
44✔
115
                ->booleanNode('enable_re_doc')->defaultValue(class_exists(TwigBundle::class))->info('Enable ReDoc')->end()
44✔
116
                ->booleanNode('enable_entrypoint')->defaultTrue()->info('Enable the entrypoint')->end()
44✔
117
                ->booleanNode('enable_docs')->defaultTrue()->info('Enable the docs')->end()
44✔
118
                ->booleanNode('enable_profiler')->defaultTrue()->info('Enable the data collector and the WebProfilerBundle integration.')->end()
44✔
119
                ->booleanNode('enable_link_security')->defaultFalse()->info('Enable security for Links (sub resources)')->end()
44✔
120
                ->arrayNode('collection')
44✔
121
                    ->addDefaultsIfNotSet()
44✔
122
                    ->children()
44✔
123
                        ->scalarNode('exists_parameter_name')->defaultValue('exists')->cannotBeEmpty()->info('The name of the query parameter to filter on nullable field values.')->end()
44✔
124
                        ->scalarNode('order')->defaultValue('ASC')->info('The default order of results.')->end() // Default ORDER is required for postgresql and mysql >= 5.7 when using LIMIT/OFFSET request
44✔
125
                        ->scalarNode('order_parameter_name')->defaultValue('order')->cannotBeEmpty()->info('The name of the query parameter to order results.')->end()
44✔
126
                        ->enumNode('order_nulls_comparison')->defaultNull()->values(interface_exists(OrderFilterInterface::class) ? array_merge(array_keys(OrderFilterInterface::NULLS_DIRECTION_MAP), [null]) : [null])->info('The nulls comparison strategy.')->end()
44✔
127
                        ->arrayNode('pagination')
44✔
128
                            ->canBeDisabled()
44✔
129
                            ->addDefaultsIfNotSet()
44✔
130
                            ->children()
44✔
131
                                ->scalarNode('page_parameter_name')->defaultValue('page')->cannotBeEmpty()->info('The default name of the parameter handling the page number.')->end()
44✔
132
                                ->scalarNode('enabled_parameter_name')->defaultValue('pagination')->cannotBeEmpty()->info('The name of the query parameter to enable or disable pagination.')->end()
44✔
133
                                ->scalarNode('items_per_page_parameter_name')->defaultValue('itemsPerPage')->cannotBeEmpty()->info('The name of the query parameter to set the number of items per page.')->end()
44✔
134
                                ->scalarNode('partial_parameter_name')->defaultValue('partial')->cannotBeEmpty()->info('The name of the query parameter to enable or disable partial pagination.')->end()
44✔
135
                            ->end()
44✔
136
                        ->end()
44✔
137
                    ->end()
44✔
138
                ->end()
44✔
139
                ->arrayNode('mapping')
44✔
140
                    ->addDefaultsIfNotSet()
44✔
141
                    ->children()
44✔
142
                        ->arrayNode('imports')
44✔
143
                            ->prototype('scalar')->end()
44✔
144
                        ->end()
44✔
145
                        ->arrayNode('paths')
44✔
146
                            ->prototype('scalar')->end()
44✔
147
                        ->end()
44✔
148
                    ->end()
44✔
149
                ->end()
44✔
150
                ->arrayNode('resource_class_directories')
44✔
151
                    ->prototype('scalar')->end()
44✔
152
                    ->setDeprecated('api-platform/symfony', '4.1', 'The "resource_class_directories" configuration is deprecated, classes using #[ApiResource] attribute are autoconfigured by the dependency injection container.')
44✔
153
                ->end()
44✔
154
                ->arrayNode('serializer')
44✔
155
                    ->addDefaultsIfNotSet()
44✔
156
                    ->children()
44✔
157
                        ->booleanNode('hydra_prefix')->defaultFalse()->info('Use the "hydra:" prefix.')->end()
44✔
158
                    ->end()
44✔
159
                ->end()
44✔
160
            ->end();
44✔
161

162
        $this->addDoctrineOrmSection($rootNode);
44✔
163
        $this->addDoctrineMongoDbOdmSection($rootNode);
44✔
164
        $this->addOAuthSection($rootNode);
44✔
165
        $this->addGraphQlSection($rootNode);
44✔
166
        $this->addSwaggerSection($rootNode);
44✔
167
        $this->addHttpCacheSection($rootNode);
44✔
168
        $this->addMercureSection($rootNode);
44✔
169
        $this->addMessengerSection($rootNode);
44✔
170
        $this->addElasticsearchSection($rootNode);
44✔
171
        $this->addOpenApiSection($rootNode);
44✔
172
        $this->addMakerSection($rootNode);
44✔
173

174
        $this->addExceptionToStatusSection($rootNode);
44✔
175

176
        $this->addFormatSection($rootNode, 'formats', [
44✔
177
            'jsonld' => ['mime_types' => ['application/ld+json']],
44✔
178
        ]);
44✔
179
        $this->addFormatSection($rootNode, 'patch_formats', [
44✔
180
            'json' => ['mime_types' => ['application/merge-patch+json']],
44✔
181
        ]);
44✔
182

183
        $defaultDocFormats = [
44✔
184
            'jsonld' => ['mime_types' => ['application/ld+json']],
44✔
185
            'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
44✔
186
            'html' => ['mime_types' => ['text/html']],
44✔
187
        ];
44✔
188

189
        if (class_exists(Yaml::class)) {
44✔
190
            $defaultDocFormats['yamlopenapi'] = ['mime_types' => ['application/vnd.openapi+yaml']];
44✔
191
        }
192

193
        $this->addFormatSection($rootNode, 'docs_formats', $defaultDocFormats);
44✔
194

195
        $this->addFormatSection($rootNode, 'error_formats', [
44✔
196
            'jsonld' => ['mime_types' => ['application/ld+json']],
44✔
197
            'jsonproblem' => ['mime_types' => ['application/problem+json']],
44✔
198
            'json' => ['mime_types' => ['application/problem+json', 'application/json']],
44✔
199
        ]);
44✔
200
        $rootNode
44✔
201
            ->children()
44✔
202
                ->arrayNode('jsonschema_formats')
44✔
203
                    ->scalarPrototype()->end()
44✔
204
                    ->defaultValue([])
44✔
205
                    ->info('The JSON formats to compute the JSON Schemas for.')
44✔
206
                ->end()
44✔
207
            ->end();
44✔
208

209
        $this->addDefaultsSection($rootNode);
44✔
210

211
        return $treeBuilder;
44✔
212
    }
213

214
    private function addDoctrineOrmSection(ArrayNodeDefinition $rootNode): void
215
    {
216
        $rootNode
44✔
217
            ->children()
44✔
218
                ->arrayNode('doctrine')
44✔
219
                    ->{class_exists(DoctrineBundle::class) && interface_exists(EntityManagerInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
44✔
220
                ->end()
44✔
221
            ->end();
44✔
222
    }
223

224
    private function addDoctrineMongoDbOdmSection(ArrayNodeDefinition $rootNode): void
225
    {
226
        $rootNode
44✔
227
            ->children()
44✔
228
                ->arrayNode('doctrine_mongodb_odm')
44✔
229
                    ->{class_exists(DoctrineMongoDBBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
44✔
230
                ->end()
44✔
231
            ->end();
44✔
232
    }
233

234
    private function addOAuthSection(ArrayNodeDefinition $rootNode): void
235
    {
236
        $rootNode
44✔
237
            ->children()
44✔
238
                ->arrayNode('oauth')
44✔
239
                    ->canBeEnabled()
44✔
240
                    ->addDefaultsIfNotSet()
44✔
241
                    ->children()
44✔
242
                        ->scalarNode('clientId')->defaultValue('')->info('The oauth client id.')->end()
44✔
243
                        ->scalarNode('clientSecret')
44✔
244
                            ->defaultValue('')
44✔
245
                            ->info('The OAuth client secret. Never use this parameter in your production environment. It exposes crucial security information. This feature is intended for dev/test environments only. Enable "oauth.pkce" instead')
44✔
246
                        ->end()
44✔
247
                        ->booleanNode('pkce')->defaultFalse()->info('Enable the oauth PKCE.')->end()
44✔
248
                        ->scalarNode('type')->defaultValue('oauth2')->info('The oauth type.')->end()
44✔
249
                        ->scalarNode('flow')->defaultValue('application')->info('The oauth flow grant type.')->end()
44✔
250
                        ->scalarNode('tokenUrl')->defaultValue('')->info('The oauth token url.')->end()
44✔
251
                        ->scalarNode('authorizationUrl')->defaultValue('')->info('The oauth authentication url.')->end()
44✔
252
                        ->scalarNode('refreshUrl')->defaultValue('')->info('The oauth refresh url.')->end()
44✔
253
                        ->arrayNode('scopes')
44✔
254
                            ->prototype('scalar')->end()
44✔
255
                        ->end()
44✔
256
                    ->end()
44✔
257
                ->end()
44✔
258
            ->end();
44✔
259
    }
260

261
    private function addGraphQlSection(ArrayNodeDefinition $rootNode): void
262
    {
263
        $rootNode
44✔
264
            ->children()
44✔
265
                ->arrayNode('graphql')
44✔
266
                    ->{class_exists(GraphQL::class) ? 'canBeDisabled' : 'canBeEnabled'}()
44✔
267
                    ->addDefaultsIfNotSet()
44✔
268
                    ->children()
44✔
269
                        ->scalarNode('default_ide')->defaultValue('graphiql')->end()
44✔
270
                        ->arrayNode('graphiql')
44✔
271
                            ->{class_exists(GraphQL::class) && class_exists(TwigBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
44✔
272
                        ->end()
44✔
273
                        ->arrayNode('introspection')
44✔
274
                            ->canBeDisabled()
44✔
275
                        ->end()
44✔
276
                        ->integerNode('max_query_depth')->defaultValue(20)
44✔
277
                        ->end()
44✔
278
                        ->arrayNode('graphql_playground')
44✔
279
                            ->setDeprecated('api-platform/core', '4.0')
44✔
280
                        ->end()
44✔
281
                        ->integerNode('max_query_complexity')->defaultValue(500)
44✔
282
                        ->end()
44✔
283
                        ->scalarNode('nesting_separator')->defaultValue('_')->info('The separator to use to filter nested fields.')->end()
44✔
284
                        ->arrayNode('collection')
44✔
285
                            ->addDefaultsIfNotSet()
44✔
286
                            ->children()
44✔
287
                                ->arrayNode('pagination')
44✔
288
                                    ->canBeDisabled()
44✔
289
                                ->end()
44✔
290
                            ->end()
44✔
291
                        ->end()
44✔
292
                    ->end()
44✔
293
                ->end()
44✔
294
            ->end();
44✔
295
    }
296

297
    private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
298
    {
299
        $supportedVersions = [3];
44✔
300

301
        $rootNode
44✔
302
            ->children()
44✔
303
                ->arrayNode('swagger')
44✔
304
                    ->addDefaultsIfNotSet()
44✔
305
                    ->children()
44✔
306
                        ->booleanNode('persist_authorization')->defaultValue(false)->info('Persist the SwaggerUI Authorization in the localStorage.')->end()
44✔
307
                        ->arrayNode('versions')
44✔
308
                            ->info('The active versions of OpenAPI to be exported or used in Swagger UI. The first value is the default.')
44✔
309
                            ->defaultValue($supportedVersions)
44✔
310
                            ->beforeNormalization()
44✔
311
                                ->always(static function ($v): array {
44✔
312
                                    if (!\is_array($v)) {
4✔
313
                                        $v = [$v];
×
314
                                    }
315

316
                                    foreach ($v as &$version) {
4✔
317
                                        $version = (int) $version;
2✔
318
                                    }
319

320
                                    return $v;
4✔
321
                                })
44✔
322
                            ->end()
44✔
323
                            ->validate()
44✔
324
                                ->ifTrue(static fn ($v): bool => $v !== array_intersect($v, $supportedVersions))
44✔
325
                                ->thenInvalid(sprintf('Only the versions %s are supported. Got %s.', implode(' and ', $supportedVersions), '%s'))
44✔
326
                            ->end()
44✔
327
                            ->prototype('scalar')->end()
44✔
328
                        ->end()
44✔
329
                        ->arrayNode('api_keys')
44✔
330
                            ->useAttributeAsKey('key')
44✔
331
                            ->validate()
44✔
332
                                ->ifTrue(static fn ($v): bool => (bool) array_filter(array_keys($v), fn ($item) => !preg_match('/^[a-zA-Z0-9._-]+$/', $item)))
44✔
333
                                ->thenInvalid('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.')
44✔
334
                            ->end()
44✔
335
                            ->prototype('array')
44✔
336
                                ->children()
44✔
337
                                    ->scalarNode('name')
44✔
338
                                        ->info('The name of the header or query parameter containing the api key.')
44✔
339
                                    ->end()
44✔
340
                                    ->enumNode('type')
44✔
341
                                        ->info('Whether the api key should be a query parameter or a header.')
44✔
342
                                        ->values(['query', 'header'])
44✔
343
                                    ->end()
44✔
344
                                ->end()
44✔
345
                            ->end()
44✔
346
                        ->end()
44✔
347
                        ->arrayNode('http_auth')
44✔
348
                            ->info('Creates http security schemes for OpenAPI.')
44✔
349
                            ->useAttributeAsKey('key')
44✔
350
                            ->validate()
44✔
351
                                ->ifTrue(static fn ($v): bool => (bool) array_filter(array_keys($v), fn ($item) => !preg_match('/^[a-zA-Z0-9._-]+$/', $item)))
44✔
352
                                ->thenInvalid('The api keys "key" is not valid according to the pattern enforced by OpenAPI 3.1 ^[a-zA-Z0-9._-]+$.')
44✔
353
                            ->end()
44✔
354
                            ->prototype('array')
44✔
355
                                ->children()
44✔
356
                                    ->scalarNode('scheme')
44✔
357
                                        ->info('The OpenAPI HTTP auth scheme, for example "bearer"')
44✔
358
                                    ->end()
44✔
359
                                    ->scalarNode('bearerFormat')
44✔
360
                                        ->info('The OpenAPI HTTP bearer format')
44✔
361
                                    ->end()
44✔
362
                                ->end()
44✔
363
                            ->end()
44✔
364
                        ->end()
44✔
365
                        ->variableNode('swagger_ui_extra_configuration')
44✔
366
                            ->defaultValue([])
44✔
367
                            ->validate()
44✔
368
                                ->ifTrue(static fn ($v): bool => false === \is_array($v))
44✔
369
                                ->thenInvalid('The swagger_ui_extra_configuration parameter must be an array.')
44✔
370
                            ->end()
44✔
371
                            ->info('To pass extra configuration to Swagger UI, like docExpansion or filter.')
44✔
372
                        ->end()
44✔
373
                    ->end()
44✔
374
                ->end()
44✔
375
            ->end();
44✔
376
    }
377

378
    private function addHttpCacheSection(ArrayNodeDefinition $rootNode): void
379
    {
380
        $rootNode
44✔
381
            ->children()
44✔
382
                ->arrayNode('http_cache')
44✔
383
                    ->addDefaultsIfNotSet()
44✔
384
                    ->children()
44✔
385
                        ->booleanNode('public')->defaultNull()->info('To make all responses public by default.')->end()
44✔
386
                        ->arrayNode('invalidation')
44✔
387
                            ->info('Enable the tags-based cache invalidation system.')
44✔
388
                            ->canBeEnabled()
44✔
389
                            ->children()
44✔
390
                                ->arrayNode('varnish_urls')
44✔
391
                                    ->setDeprecated('api-platform/core', '3.0', 'The "varnish_urls" configuration is deprecated, use "urls" or "scoped_clients".')
44✔
392
                                    ->defaultValue([])
44✔
393
                                    ->prototype('scalar')->end()
44✔
394
                                    ->info('URLs of the Varnish servers to purge using cache tags when a resource is updated.')
44✔
395
                                ->end()
44✔
396
                                ->arrayNode('urls')
44✔
397
                                    ->defaultValue([])
44✔
398
                                    ->prototype('scalar')->end()
44✔
399
                                    ->info('URLs of the Varnish servers to purge using cache tags when a resource is updated.')
44✔
400
                                ->end()
44✔
401
                                ->arrayNode('scoped_clients')
44✔
402
                                    ->defaultValue([])
44✔
403
                                    ->prototype('scalar')->end()
44✔
404
                                    ->info('Service names of scoped client to use by the cache purger.')
44✔
405
                                ->end()
44✔
406
                                ->integerNode('max_header_length')
44✔
407
                                    ->defaultValue(7500)
44✔
408
                                    ->info('Max header length supported by the cache server.')
44✔
409
                                ->end()
44✔
410
                                ->variableNode('request_options')
44✔
411
                                    ->defaultValue([])
44✔
412
                                    ->validate()
44✔
413
                                        ->ifTrue(static fn ($v): bool => false === \is_array($v))
44✔
414
                                        ->thenInvalid('The request_options parameter must be an array.')
44✔
415
                                    ->end()
44✔
416
                                    ->info('To pass options to the client charged with the request.')
44✔
417
                                ->end()
44✔
418
                                ->scalarNode('purger')
44✔
419
                                    ->defaultValue('api_platform.http_cache.purger.varnish')
44✔
420
                                    ->info('Specify a purger to use (available values: "api_platform.http_cache.purger.varnish.ban", "api_platform.http_cache.purger.varnish.xkey", "api_platform.http_cache.purger.souin").')
44✔
421
                                ->end()
44✔
422
                                ->arrayNode('xkey')
44✔
423
                                    ->setDeprecated('api-platform/core', '3.0', 'The "xkey" configuration is deprecated, use your own purger to customize surrogate keys or the appropriate paramters.')
44✔
424
                                    ->addDefaultsIfNotSet()
44✔
425
                                    ->children()
44✔
426
                                        ->scalarNode('glue')
44✔
427
                                        ->defaultValue(' ')
44✔
428
                                        ->info('xkey glue between keys')
44✔
429
                                        ->end()
44✔
430
                                    ->end()
44✔
431
                                ->end()
44✔
432
                            ->end()
44✔
433
                        ->end()
44✔
434
                    ->end()
44✔
435
                ->end()
44✔
436
            ->end();
44✔
437
    }
438

439
    private function addMercureSection(ArrayNodeDefinition $rootNode): void
440
    {
441
        $rootNode
44✔
442
            ->children()
44✔
443
                ->arrayNode('mercure')
44✔
444
                    ->{class_exists(MercureBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
44✔
445
                    ->children()
44✔
446
                        ->scalarNode('hub_url')
44✔
447
                            ->defaultNull()
44✔
448
                            ->info('The URL sent in the Link HTTP header. If not set, will default to the URL for MercureBundle\'s default hub.')
44✔
449
                        ->end()
44✔
450
                        ->booleanNode('include_type')
44✔
451
                            ->defaultFalse()
44✔
452
                            ->info('Always include @type in updates (including delete ones).')
44✔
453
                        ->end()
44✔
454
                    ->end()
44✔
455
                ->end()
44✔
456
            ->end();
44✔
457
    }
458

459
    private function addMessengerSection(ArrayNodeDefinition $rootNode): void
460
    {
461
        $rootNode
44✔
462
            ->children()
44✔
463
                ->arrayNode('messenger')
44✔
464
                    ->{!class_exists(FullStack::class) && interface_exists(MessageBusInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
44✔
465
                ->end()
44✔
466
            ->end();
44✔
467
    }
468

469
    private function addElasticsearchSection(ArrayNodeDefinition $rootNode): void
470
    {
471
        $rootNode
44✔
472
            ->children()
44✔
473
                ->arrayNode('elasticsearch')
44✔
474
                    ->canBeEnabled()
44✔
475
                    ->addDefaultsIfNotSet()
44✔
476
                    ->children()
44✔
477
                        ->booleanNode('enabled')
44✔
478
                            ->defaultFalse()
44✔
479
                            ->validate()
44✔
480
                                ->ifTrue()
44✔
481
                                ->then(static function (bool $v): bool {
44✔
482
                                    if (
483
                                        // ES v7
484
                                        !class_exists(\Elasticsearch\Client::class)
2✔
485
                                        // ES v8 and up
486
                                        && !class_exists(\Elastic\Elasticsearch\Client::class)
2✔
487
                                    ) {
488
                                        throw new InvalidConfigurationException('The elasticsearch/elasticsearch package is required for Elasticsearch support.');
×
489
                                    }
490

491
                                    return $v;
2✔
492
                                })
44✔
493
                            ->end()
44✔
494
                        ->end()
44✔
495
                        ->arrayNode('hosts')
44✔
496
                            ->beforeNormalization()->castToArray()->end()
44✔
497
                            ->defaultValue([])
44✔
498
                            ->prototype('scalar')->end()
44✔
499
                        ->end()
44✔
500
                    ->end()
44✔
501
                ->end()
44✔
502
            ->end();
44✔
503
    }
504

505
    private function addOpenApiSection(ArrayNodeDefinition $rootNode): void
506
    {
507
        $rootNode
44✔
508
            ->children()
44✔
509
                ->arrayNode('openapi')
44✔
510
                    ->addDefaultsIfNotSet()
44✔
511
                        ->children()
44✔
512
                        ->arrayNode('contact')
44✔
513
                        ->addDefaultsIfNotSet()
44✔
514
                            ->children()
44✔
515
                                ->scalarNode('name')->defaultNull()->info('The identifying name of the contact person/organization.')->end()
44✔
516
                                ->scalarNode('url')->defaultNull()->info('The URL pointing to the contact information. MUST be in the format of a URL.')->end()
44✔
517
                                ->scalarNode('email')->defaultNull()->info('The email address of the contact person/organization. MUST be in the format of an email address.')->end()
44✔
518
                            ->end()
44✔
519
                        ->end()
44✔
520
                        ->scalarNode('termsOfService')->defaultNull()->info('A URL to the Terms of Service for the API. MUST be in the format of a URL.')->end()
44✔
521
                        ->arrayNode('tags')
44✔
522
                            ->info('Global OpenApi tags overriding the default computed tags if specified.')
44✔
523
                            ->prototype('array')
44✔
524
                                ->children()
44✔
525
                                    ->scalarNode('name')->isRequired()->end()
44✔
526
                                    ->scalarNode('description')->defaultNull()->end()
44✔
527
                                ->end()
44✔
528
                            ->end()
44✔
529
                        ->end()
44✔
530
                        ->arrayNode('license')
44✔
531
                        ->addDefaultsIfNotSet()
44✔
532
                            ->children()
44✔
533
                                ->scalarNode('name')->defaultNull()->info('The license name used for the API.')->end()
44✔
534
                                ->scalarNode('url')->defaultNull()->info('URL to the license used for the API. MUST be in the format of a URL.')->end()
44✔
535
                                ->scalarNode('identifier')->defaultNull()->info('An SPDX license expression for the API. The identifier field is mutually exclusive of the url field.')->end()
44✔
536
                            ->end()
44✔
537
                        ->end()
44✔
538
                        ->variableNode('swagger_ui_extra_configuration')
44✔
539
                            ->defaultValue([])
44✔
540
                            ->validate()
44✔
541
                                ->ifTrue(static fn ($v): bool => false === \is_array($v))
44✔
542
                                ->thenInvalid('The swagger_ui_extra_configuration parameter must be an array.')
44✔
543
                            ->end()
44✔
544
                            ->info('To pass extra configuration to Swagger UI, like docExpansion or filter.')
44✔
545
                        ->end()
44✔
546
                        ->booleanNode('overrideResponses')->defaultTrue()->info('Whether API Platform adds automatic responses to the OpenAPI documentation.')->end()
44✔
547
                        ->scalarNode('error_resource_class')->defaultNull()->info('The class used to represent errors in the OpenAPI documentation.')->end()
44✔
548
                        ->scalarNode('validation_error_resource_class')->defaultNull()->info('The class used to represent validation errors in the OpenAPI documentation.')->end()
44✔
549
                    ->end()
44✔
550
                ->end()
44✔
551
            ->end();
44✔
552
    }
553

554
    /**
555
     * @throws InvalidConfigurationException
556
     */
557
    private function addExceptionToStatusSection(ArrayNodeDefinition $rootNode): void
558
    {
559
        $rootNode
44✔
560
            ->children()
44✔
561
                ->arrayNode('exception_to_status')
44✔
562
                    ->defaultValue([
44✔
563
                        SerializerExceptionInterface::class => Response::HTTP_BAD_REQUEST,
44✔
564
                        InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
44✔
565
                        OptimisticLockException::class => Response::HTTP_CONFLICT,
44✔
566
                    ])
44✔
567
                    ->info('The list of exceptions mapped to their HTTP status code.')
44✔
568
                    ->normalizeKeys(false)
44✔
569
                    ->useAttributeAsKey('exception_class')
44✔
570
                    ->prototype('integer')->end()
44✔
571
                    ->validate()
44✔
572
                        ->ifArray()
44✔
573
                        ->then(static function (array $exceptionToStatus): array {
44✔
574
                            foreach ($exceptionToStatus as $httpStatusCode) {
12✔
575
                                if ($httpStatusCode < 100 || $httpStatusCode >= 600) {
12✔
576
                                    throw new InvalidConfigurationException(sprintf('The HTTP status code "%s" is not valid.', $httpStatusCode));
8✔
577
                                }
578
                            }
579

580
                            return $exceptionToStatus;
4✔
581
                        })
44✔
582
                    ->end()
44✔
583
                ->end()
44✔
584
            ->end();
44✔
585
    }
586

587
    private function addFormatSection(ArrayNodeDefinition $rootNode, string $key, array $defaultValue): void
588
    {
589
        $rootNode
44✔
590
            ->children()
44✔
591
                ->arrayNode($key)
44✔
592
                    ->defaultValue($defaultValue)
44✔
593
                    ->info('The list of enabled formats. The first one will be the default.')
44✔
594
                    ->normalizeKeys(false)
44✔
595
                    ->useAttributeAsKey('format')
44✔
596
                    ->beforeNormalization()
44✔
597
                        ->ifArray()
44✔
598
                        ->then(static function ($v) {
44✔
599
                            foreach ($v as $format => $value) {
4✔
600
                                if (isset($value['mime_types'])) {
4✔
601
                                    continue;
×
602
                                }
603

604
                                $v[$format] = ['mime_types' => $value];
4✔
605
                            }
606

607
                            return $v;
4✔
608
                        })
44✔
609
                    ->end()
44✔
610
                    ->prototype('array')
44✔
611
                        ->children()
44✔
612
                            ->arrayNode('mime_types')->prototype('scalar')->end()->end()
44✔
613
                        ->end()
44✔
614
                    ->end()
44✔
615
                ->end()
44✔
616
            ->end();
44✔
617
    }
618

619
    private function addDefaultsSection(ArrayNodeDefinition $rootNode): void
620
    {
621
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
44✔
622
        $defaultsNode = $rootNode->children()->arrayNode('defaults');
44✔
623

624
        $defaultsNode
44✔
625
            ->ignoreExtraKeys(false)
44✔
626
            ->beforeNormalization()
44✔
627
            ->always(static function (array $defaults) use ($nameConverter): array {
44✔
628
                $normalizedDefaults = [];
4✔
629
                foreach ($defaults as $option => $value) {
4✔
630
                    $option = $nameConverter->normalize($option);
4✔
631
                    $normalizedDefaults[$option] = $value;
4✔
632
                }
633

634
                return $normalizedDefaults;
4✔
635
            });
44✔
636

637
        $this->defineDefault($defaultsNode, new \ReflectionClass(ApiResource::class), $nameConverter);
44✔
638
        $this->defineDefault($defaultsNode, new \ReflectionClass(Put::class), $nameConverter);
44✔
639
        $this->defineDefault($defaultsNode, new \ReflectionClass(Post::class), $nameConverter);
44✔
640
    }
641

642
    private function addMakerSection(ArrayNodeDefinition $rootNode): void
643
    {
644
        $rootNode
44✔
645
            ->children()
44✔
646
                ->arrayNode('maker')
44✔
647
                    ->{class_exists(MakerBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
44✔
648
                ->end()
44✔
649
            ->end();
44✔
650
    }
651

652
    private function defineDefault(ArrayNodeDefinition $defaultsNode, \ReflectionClass $reflectionClass, CamelCaseToSnakeCaseNameConverter $nameConverter): void
653
    {
654
        foreach ($reflectionClass->getConstructor()->getParameters() as $parameter) {
44✔
655
            $defaultsNode->children()->variableNode($nameConverter->normalize($parameter->getName()));
44✔
656
        }
657
    }
658
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc