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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

99.39
/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\OpenApi\Model\Tag;
22
use ApiPlatform\Symfony\Controller\MainController;
23
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
24
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
25
use Doctrine\ORM\EntityManagerInterface;
26
use Doctrine\ORM\OptimisticLockException;
27
use GraphQL\GraphQL;
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\Messenger\MessageBusInterface;
38
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
39
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
40
use Symfony\Component\Yaml\Yaml;
41

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

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

UNCOV
66
                    return $v;
1✔
UNCOV
67
                })
21✔
UNCOV
68
            ->end()
21✔
UNCOV
69
            ->children()
21✔
UNCOV
70
                ->scalarNode('title')
21✔
UNCOV
71
                    ->info('The title of the API.')
21✔
UNCOV
72
                    ->cannotBeEmpty()
21✔
UNCOV
73
                    ->defaultValue('')
21✔
UNCOV
74
                ->end()
21✔
UNCOV
75
                ->scalarNode('description')
21✔
UNCOV
76
                    ->info('The description of the API.')
21✔
UNCOV
77
                    ->cannotBeEmpty()
21✔
UNCOV
78
                    ->defaultValue('')
21✔
UNCOV
79
                ->end()
21✔
UNCOV
80
                ->scalarNode('version')
21✔
UNCOV
81
                    ->info('The version of the API.')
21✔
UNCOV
82
                    ->cannotBeEmpty()
21✔
UNCOV
83
                    ->defaultValue('0.0.0')
21✔
UNCOV
84
                ->end()
21✔
UNCOV
85
                ->booleanNode('show_webby')->defaultTrue()->info('If true, show Webby on the documentation page')->end()
21✔
UNCOV
86
                ->booleanNode('use_symfony_listeners')->defaultFalse()->info(sprintf('Uses Symfony event listeners instead of the %s.', MainController::class))->end()
21✔
UNCOV
87
                ->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end()
21✔
UNCOV
88
                ->scalarNode('asset_package')->defaultNull()->info('Specify an asset package name to use.')->end()
21✔
UNCOV
89
                ->scalarNode('path_segment_name_generator')->defaultValue('api_platform.metadata.path_segment_name_generator.underscore')->info('Specify a path name generator to use.')->end()
21✔
UNCOV
90
                ->scalarNode('inflector')->defaultValue('api_platform.metadata.inflector')->info('Specify an inflector to use.')->end()
21✔
UNCOV
91
                ->arrayNode('validator')
21✔
UNCOV
92
                    ->addDefaultsIfNotSet()
21✔
UNCOV
93
                    ->children()
21✔
UNCOV
94
                        ->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()
21✔
UNCOV
95
                        ->booleanNode('query_parameter_validation')->defaultValue(true)->end()
21✔
UNCOV
96
                    ->end()
21✔
UNCOV
97
                ->end()
21✔
UNCOV
98
                ->arrayNode('eager_loading')
21✔
UNCOV
99
                    ->canBeDisabled()
21✔
UNCOV
100
                    ->addDefaultsIfNotSet()
21✔
UNCOV
101
                    ->children()
21✔
UNCOV
102
                        ->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()
21✔
UNCOV
103
                        ->integerNode('max_joins')->defaultValue(30)->info('Max number of joined relations before EagerLoading throws a RuntimeException')->end()
21✔
UNCOV
104
                        ->booleanNode('force_eager')->defaultTrue()->info('Force join on every relation. If disabled, it will only join relations having the EAGER fetch mode.')->end()
21✔
UNCOV
105
                    ->end()
21✔
UNCOV
106
                ->end()
21✔
UNCOV
107
                ->booleanNode('handle_symfony_errors')->defaultFalse()->info('Allows to handle symfony exceptions.')->end()
21✔
UNCOV
108
                ->booleanNode('enable_swagger')->defaultTrue()->info('Enable the Swagger documentation and export.')->end()
21✔
UNCOV
109
                ->booleanNode('enable_swagger_ui')->defaultValue(class_exists(TwigBundle::class))->info('Enable Swagger UI')->end()
21✔
UNCOV
110
                ->booleanNode('enable_re_doc')->defaultValue(class_exists(TwigBundle::class))->info('Enable ReDoc')->end()
21✔
UNCOV
111
                ->booleanNode('enable_entrypoint')->defaultTrue()->info('Enable the entrypoint')->end()
21✔
UNCOV
112
                ->booleanNode('enable_docs')->defaultTrue()->info('Enable the docs')->end()
21✔
UNCOV
113
                ->booleanNode('enable_profiler')->defaultTrue()->info('Enable the data collector and the WebProfilerBundle integration.')->end()
21✔
UNCOV
114
                ->booleanNode('enable_link_security')->defaultFalse()->info('Enable security for Links (sub resources)')->end()
21✔
UNCOV
115
                ->arrayNode('collection')
21✔
UNCOV
116
                    ->addDefaultsIfNotSet()
21✔
UNCOV
117
                    ->children()
21✔
UNCOV
118
                        ->scalarNode('exists_parameter_name')->defaultValue('exists')->cannotBeEmpty()->info('The name of the query parameter to filter on nullable field values.')->end()
21✔
UNCOV
119
                        ->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
21✔
UNCOV
120
                        ->scalarNode('order_parameter_name')->defaultValue('order')->cannotBeEmpty()->info('The name of the query parameter to order results.')->end()
21✔
UNCOV
121
                        ->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()
21✔
UNCOV
122
                        ->arrayNode('pagination')
21✔
UNCOV
123
                            ->canBeDisabled()
21✔
UNCOV
124
                            ->addDefaultsIfNotSet()
21✔
UNCOV
125
                            ->children()
21✔
UNCOV
126
                                ->scalarNode('page_parameter_name')->defaultValue('page')->cannotBeEmpty()->info('The default name of the parameter handling the page number.')->end()
21✔
UNCOV
127
                                ->scalarNode('enabled_parameter_name')->defaultValue('pagination')->cannotBeEmpty()->info('The name of the query parameter to enable or disable pagination.')->end()
21✔
UNCOV
128
                                ->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()
21✔
UNCOV
129
                                ->scalarNode('partial_parameter_name')->defaultValue('partial')->cannotBeEmpty()->info('The name of the query parameter to enable or disable partial pagination.')->end()
21✔
UNCOV
130
                            ->end()
21✔
UNCOV
131
                        ->end()
21✔
UNCOV
132
                    ->end()
21✔
UNCOV
133
                ->end()
21✔
UNCOV
134
                ->arrayNode('mapping')
21✔
UNCOV
135
                    ->addDefaultsIfNotSet()
21✔
UNCOV
136
                    ->children()
21✔
UNCOV
137
                        ->arrayNode('paths')
21✔
UNCOV
138
                            ->prototype('scalar')->end()
21✔
UNCOV
139
                        ->end()
21✔
UNCOV
140
                    ->end()
21✔
UNCOV
141
                ->end()
21✔
UNCOV
142
                ->arrayNode('resource_class_directories')
21✔
UNCOV
143
                    ->prototype('scalar')->end()
21✔
UNCOV
144
                ->end()
21✔
UNCOV
145
                ->arrayNode('serializer')
21✔
UNCOV
146
                    ->addDefaultsIfNotSet()
21✔
UNCOV
147
                    ->children()
21✔
UNCOV
148
                        ->booleanNode('hydra_prefix')->defaultFalse()->info('Use the "hydra:" prefix.')->end()
21✔
UNCOV
149
                    ->end()
21✔
UNCOV
150
                ->end()
21✔
UNCOV
151
            ->end();
21✔
152

UNCOV
153
        $this->addDoctrineOrmSection($rootNode);
21✔
UNCOV
154
        $this->addDoctrineMongoDbOdmSection($rootNode);
21✔
UNCOV
155
        $this->addOAuthSection($rootNode);
21✔
UNCOV
156
        $this->addGraphQlSection($rootNode);
21✔
UNCOV
157
        $this->addSwaggerSection($rootNode);
21✔
UNCOV
158
        $this->addHttpCacheSection($rootNode);
21✔
UNCOV
159
        $this->addMercureSection($rootNode);
21✔
UNCOV
160
        $this->addMessengerSection($rootNode);
21✔
UNCOV
161
        $this->addElasticsearchSection($rootNode);
21✔
UNCOV
162
        $this->addOpenApiSection($rootNode);
21✔
UNCOV
163
        $this->addMakerSection($rootNode);
21✔
164

UNCOV
165
        $this->addExceptionToStatusSection($rootNode);
21✔
166

UNCOV
167
        $this->addFormatSection($rootNode, 'formats', [
21✔
UNCOV
168
            'jsonld' => ['mime_types' => ['application/ld+json']],
21✔
UNCOV
169
        ]);
21✔
UNCOV
170
        $this->addFormatSection($rootNode, 'patch_formats', [
21✔
UNCOV
171
            'json' => ['mime_types' => ['application/merge-patch+json']],
21✔
UNCOV
172
        ]);
21✔
173

UNCOV
174
        $defaultDocFormats = [
21✔
UNCOV
175
            'jsonld' => ['mime_types' => ['application/ld+json']],
21✔
UNCOV
176
            'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
21✔
UNCOV
177
            'html' => ['mime_types' => ['text/html']],
21✔
UNCOV
178
        ];
21✔
179

UNCOV
180
        if (class_exists(Yaml::class)) {
21✔
UNCOV
181
            $defaultDocFormats['yamlopenapi'] = ['mime_types' => ['application/vnd.openapi+yaml']];
21✔
182
        }
183

UNCOV
184
        $this->addFormatSection($rootNode, 'docs_formats', $defaultDocFormats);
21✔
185

UNCOV
186
        $this->addFormatSection($rootNode, 'error_formats', [
21✔
UNCOV
187
            'jsonld' => ['mime_types' => ['application/ld+json']],
21✔
UNCOV
188
            'jsonproblem' => ['mime_types' => ['application/problem+json']],
21✔
UNCOV
189
            'json' => ['mime_types' => ['application/problem+json', 'application/json']],
21✔
UNCOV
190
        ]);
21✔
UNCOV
191
        $rootNode
21✔
UNCOV
192
            ->children()
21✔
UNCOV
193
                ->arrayNode('jsonschema_formats')
21✔
UNCOV
194
                    ->scalarPrototype()->end()
21✔
UNCOV
195
                    ->defaultValue([])
21✔
UNCOV
196
                    ->info('The JSON formats to compute the JSON Schemas for.')
21✔
UNCOV
197
                ->end()
21✔
UNCOV
198
            ->end();
21✔
199

UNCOV
200
        $this->addDefaultsSection($rootNode);
21✔
201

UNCOV
202
        return $treeBuilder;
21✔
203
    }
204

205
    private function addDoctrineOrmSection(ArrayNodeDefinition $rootNode): void
206
    {
UNCOV
207
        $rootNode
21✔
UNCOV
208
            ->children()
21✔
UNCOV
209
                ->arrayNode('doctrine')
21✔
UNCOV
210
                    ->{class_exists(DoctrineBundle::class) && interface_exists(EntityManagerInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
21✔
UNCOV
211
                ->end()
21✔
UNCOV
212
            ->end();
21✔
213
    }
214

215
    private function addDoctrineMongoDbOdmSection(ArrayNodeDefinition $rootNode): void
216
    {
UNCOV
217
        $rootNode
21✔
UNCOV
218
            ->children()
21✔
UNCOV
219
                ->arrayNode('doctrine_mongodb_odm')
21✔
UNCOV
220
                    ->{class_exists(DoctrineMongoDBBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
21✔
UNCOV
221
                ->end()
21✔
UNCOV
222
            ->end();
21✔
223
    }
224

225
    private function addOAuthSection(ArrayNodeDefinition $rootNode): void
226
    {
UNCOV
227
        $rootNode
21✔
UNCOV
228
            ->children()
21✔
UNCOV
229
                ->arrayNode('oauth')
21✔
UNCOV
230
                    ->canBeEnabled()
21✔
UNCOV
231
                    ->addDefaultsIfNotSet()
21✔
UNCOV
232
                    ->children()
21✔
UNCOV
233
                        ->scalarNode('clientId')->defaultValue('')->info('The oauth client id.')->end()
21✔
UNCOV
234
                        ->scalarNode('clientSecret')
21✔
UNCOV
235
                            ->defaultValue('')
21✔
UNCOV
236
                            ->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')
21✔
UNCOV
237
                        ->end()
21✔
UNCOV
238
                        ->booleanNode('pkce')->defaultFalse()->info('Enable the oauth PKCE.')->end()
21✔
UNCOV
239
                        ->scalarNode('type')->defaultValue('oauth2')->info('The oauth type.')->end()
21✔
UNCOV
240
                        ->scalarNode('flow')->defaultValue('application')->info('The oauth flow grant type.')->end()
21✔
UNCOV
241
                        ->scalarNode('tokenUrl')->defaultValue('')->info('The oauth token url.')->end()
21✔
UNCOV
242
                        ->scalarNode('authorizationUrl')->defaultValue('')->info('The oauth authentication url.')->end()
21✔
UNCOV
243
                        ->scalarNode('refreshUrl')->defaultValue('')->info('The oauth refresh url.')->end()
21✔
UNCOV
244
                        ->arrayNode('scopes')
21✔
UNCOV
245
                            ->prototype('scalar')->end()
21✔
UNCOV
246
                        ->end()
21✔
UNCOV
247
                    ->end()
21✔
UNCOV
248
                ->end()
21✔
UNCOV
249
            ->end();
21✔
250
    }
251

252
    private function addGraphQlSection(ArrayNodeDefinition $rootNode): void
253
    {
UNCOV
254
        $rootNode
21✔
UNCOV
255
            ->children()
21✔
UNCOV
256
                ->arrayNode('graphql')
21✔
UNCOV
257
                    ->{class_exists(GraphQL::class) ? 'canBeDisabled' : 'canBeEnabled'}()
21✔
UNCOV
258
                    ->addDefaultsIfNotSet()
21✔
UNCOV
259
                    ->children()
21✔
UNCOV
260
                        ->scalarNode('default_ide')->defaultValue('graphiql')->end()
21✔
UNCOV
261
                        ->arrayNode('graphiql')
21✔
UNCOV
262
                            ->{class_exists(GraphQL::class) && class_exists(TwigBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
21✔
UNCOV
263
                        ->end()
21✔
UNCOV
264
                        ->arrayNode('graphql_playground')
21✔
UNCOV
265
                            ->{class_exists(GraphQL::class) && class_exists(TwigBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
21✔
UNCOV
266
                        ->end()
21✔
UNCOV
267
                        ->arrayNode('introspection')
21✔
UNCOV
268
                            ->canBeDisabled()
21✔
UNCOV
269
                        ->end()
21✔
UNCOV
270
                        ->integerNode('max_query_depth')->defaultValue(20)
21✔
UNCOV
271
                        ->end()
21✔
UNCOV
272
                        ->integerNode('max_query_complexity')->defaultValue(500)
21✔
UNCOV
273
                        ->end()
21✔
UNCOV
274
                        ->scalarNode('nesting_separator')->defaultValue('_')->info('The separator to use to filter nested fields.')->end()
21✔
UNCOV
275
                        ->arrayNode('collection')
21✔
UNCOV
276
                            ->addDefaultsIfNotSet()
21✔
UNCOV
277
                            ->children()
21✔
UNCOV
278
                                ->arrayNode('pagination')
21✔
UNCOV
279
                                    ->canBeDisabled()
21✔
UNCOV
280
                                ->end()
21✔
UNCOV
281
                            ->end()
21✔
UNCOV
282
                        ->end()
21✔
UNCOV
283
                    ->end()
21✔
UNCOV
284
                ->end()
21✔
UNCOV
285
            ->end();
21✔
286
    }
287

288
    private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
289
    {
UNCOV
290
        $supportedVersions = [3];
21✔
291

UNCOV
292
        $rootNode
21✔
UNCOV
293
            ->children()
21✔
UNCOV
294
                ->arrayNode('swagger')
21✔
UNCOV
295
                    ->addDefaultsIfNotSet()
21✔
UNCOV
296
                    ->children()
21✔
UNCOV
297
                        ->booleanNode('persist_authorization')->defaultValue(false)->info('Persist the SwaggerUI Authorization in the localStorage.')->end()
21✔
UNCOV
298
                        ->arrayNode('versions')
21✔
UNCOV
299
                            ->info('The active versions of OpenAPI to be exported or used in Swagger UI. The first value is the default.')
21✔
UNCOV
300
                            ->defaultValue($supportedVersions)
21✔
UNCOV
301
                            ->beforeNormalization()
21✔
UNCOV
302
                                ->always(static function ($v): array {
21✔
UNCOV
303
                                    if (!\is_array($v)) {
2✔
304
                                        $v = [$v];
×
305
                                    }
306

UNCOV
307
                                    foreach ($v as &$version) {
2✔
UNCOV
308
                                        $version = (int) $version;
1✔
309
                                    }
310

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

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

430
    private function addMercureSection(ArrayNodeDefinition $rootNode): void
431
    {
UNCOV
432
        $rootNode
21✔
UNCOV
433
            ->children()
21✔
UNCOV
434
                ->arrayNode('mercure')
21✔
UNCOV
435
                    ->{class_exists(MercureBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
21✔
UNCOV
436
                    ->children()
21✔
UNCOV
437
                        ->scalarNode('hub_url')
21✔
UNCOV
438
                            ->defaultNull()
21✔
UNCOV
439
                            ->info('The URL sent in the Link HTTP header. If not set, will default to the URL for MercureBundle\'s default hub.')
21✔
UNCOV
440
                        ->end()
21✔
UNCOV
441
                        ->booleanNode('include_type')
21✔
UNCOV
442
                            ->defaultFalse()
21✔
UNCOV
443
                            ->info('Always include @type in updates (including delete ones).')
21✔
UNCOV
444
                        ->end()
21✔
UNCOV
445
                    ->end()
21✔
UNCOV
446
                ->end()
21✔
UNCOV
447
            ->end();
21✔
448
    }
449

450
    private function addMessengerSection(ArrayNodeDefinition $rootNode): void
451
    {
UNCOV
452
        $rootNode
21✔
UNCOV
453
            ->children()
21✔
UNCOV
454
                ->arrayNode('messenger')
21✔
UNCOV
455
                    ->{!class_exists(FullStack::class) && interface_exists(MessageBusInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
21✔
UNCOV
456
                ->end()
21✔
UNCOV
457
            ->end();
21✔
458
    }
459

460
    private function addElasticsearchSection(ArrayNodeDefinition $rootNode): void
461
    {
UNCOV
462
        $rootNode
21✔
UNCOV
463
            ->children()
21✔
UNCOV
464
                ->arrayNode('elasticsearch')
21✔
UNCOV
465
                    ->canBeEnabled()
21✔
UNCOV
466
                    ->addDefaultsIfNotSet()
21✔
UNCOV
467
                    ->children()
21✔
UNCOV
468
                        ->booleanNode('enabled')
21✔
UNCOV
469
                            ->defaultFalse()
21✔
UNCOV
470
                            ->validate()
21✔
UNCOV
471
                                ->ifTrue()
21✔
UNCOV
472
                                ->then(static function (bool $v): bool {
21✔
473
                                    if (
474
                                        // ES v7
UNCOV
475
                                        !class_exists(\Elasticsearch\Client::class)
1✔
476
                                        // ES v8 and up
UNCOV
477
                                        && !class_exists(\Elastic\Elasticsearch\Client::class)
1✔
478
                                    ) {
479
                                        throw new InvalidConfigurationException('The elasticsearch/elasticsearch package is required for Elasticsearch support.');
×
480
                                    }
481

UNCOV
482
                                    return $v;
1✔
UNCOV
483
                                })
21✔
UNCOV
484
                            ->end()
21✔
UNCOV
485
                        ->end()
21✔
UNCOV
486
                        ->arrayNode('hosts')
21✔
UNCOV
487
                            ->beforeNormalization()->castToArray()->end()
21✔
UNCOV
488
                            ->defaultValue([])
21✔
UNCOV
489
                            ->prototype('scalar')->end()
21✔
UNCOV
490
                        ->end()
21✔
UNCOV
491
                    ->end()
21✔
UNCOV
492
                ->end()
21✔
UNCOV
493
            ->end();
21✔
494
    }
495

496
    private function addOpenApiSection(ArrayNodeDefinition $rootNode): void
497
    {
UNCOV
498
        $rootNode
21✔
UNCOV
499
            ->children()
21✔
UNCOV
500
                ->arrayNode('openapi')
21✔
UNCOV
501
                    ->addDefaultsIfNotSet()
21✔
UNCOV
502
                        ->children()
21✔
UNCOV
503
                        ->arrayNode('contact')
21✔
UNCOV
504
                        ->addDefaultsIfNotSet()
21✔
UNCOV
505
                            ->children()
21✔
UNCOV
506
                                ->scalarNode('name')->defaultNull()->info('The identifying name of the contact person/organization.')->end()
21✔
UNCOV
507
                                ->scalarNode('url')->defaultNull()->info('The URL pointing to the contact information. MUST be in the format of a URL.')->end()
21✔
UNCOV
508
                                ->scalarNode('email')->defaultNull()->info('The email address of the contact person/organization. MUST be in the format of an email address.')->end()
21✔
UNCOV
509
                            ->end()
21✔
UNCOV
510
                        ->end()
21✔
UNCOV
511
                        ->scalarNode('termsOfService')->defaultNull()->info('A URL to the Terms of Service for the API. MUST be in the format of a URL.')->end()
21✔
UNCOV
512
                        ->arrayNode('tags')
21✔
UNCOV
513
                            ->info('Global OpenApi tags overriding the default computed tags if specified.')
21✔
UNCOV
514
                            ->prototype('array')
21✔
UNCOV
515
                                ->children()
21✔
UNCOV
516
                                    ->scalarNode('name')->isRequired()->end()
21✔
UNCOV
517
                                    ->scalarNode('description')->defaultNull()->end()
21✔
UNCOV
518
                                ->end()
21✔
UNCOV
519
                            ->end()
21✔
UNCOV
520
                        ->end()
21✔
UNCOV
521
                        ->arrayNode('license')
21✔
UNCOV
522
                        ->addDefaultsIfNotSet()
21✔
UNCOV
523
                            ->children()
21✔
UNCOV
524
                                ->scalarNode('name')->defaultNull()->info('The license name used for the API.')->end()
21✔
UNCOV
525
                                ->scalarNode('url')->defaultNull()->info('URL to the license used for the API. MUST be in the format of a URL.')->end()
21✔
UNCOV
526
                            ->end()
21✔
UNCOV
527
                        ->end()
21✔
UNCOV
528
                        ->variableNode('swagger_ui_extra_configuration')
21✔
UNCOV
529
                            ->defaultValue([])
21✔
UNCOV
530
                            ->validate()
21✔
UNCOV
531
                                ->ifTrue(static fn ($v): bool => false === \is_array($v))
21✔
UNCOV
532
                                ->thenInvalid('The swagger_ui_extra_configuration parameter must be an array.')
21✔
UNCOV
533
                            ->end()
21✔
UNCOV
534
                            ->info('To pass extra configuration to Swagger UI, like docExpansion or filter.')
21✔
UNCOV
535
                        ->end()
21✔
UNCOV
536
                        ->booleanNode('overrideResponses')->defaultTrue()->info('Whether API Platform adds automatic responses to the OpenAPI documentation.')
21✔
UNCOV
537
                    ->end()
21✔
UNCOV
538
                ->end()
21✔
UNCOV
539
            ->end();
21✔
540
    }
541

542
    /**
543
     * @throws InvalidConfigurationException
544
     */
545
    private function addExceptionToStatusSection(ArrayNodeDefinition $rootNode): void
546
    {
UNCOV
547
        $rootNode
21✔
UNCOV
548
            ->children()
21✔
UNCOV
549
                ->arrayNode('exception_to_status')
21✔
UNCOV
550
                    ->defaultValue([
21✔
UNCOV
551
                        SerializerExceptionInterface::class => Response::HTTP_BAD_REQUEST,
21✔
UNCOV
552
                        InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
21✔
UNCOV
553
                        OptimisticLockException::class => Response::HTTP_CONFLICT,
21✔
UNCOV
554
                    ])
21✔
UNCOV
555
                    ->info('The list of exceptions mapped to their HTTP status code.')
21✔
UNCOV
556
                    ->normalizeKeys(false)
21✔
UNCOV
557
                    ->useAttributeAsKey('exception_class')
21✔
UNCOV
558
                    ->prototype('integer')->end()
21✔
UNCOV
559
                    ->validate()
21✔
UNCOV
560
                        ->ifArray()
21✔
UNCOV
561
                        ->then(static function (array $exceptionToStatus): array {
21✔
UNCOV
562
                            foreach ($exceptionToStatus as $httpStatusCode) {
5✔
UNCOV
563
                                if ($httpStatusCode < 100 || $httpStatusCode >= 600) {
5✔
UNCOV
564
                                    throw new InvalidConfigurationException(sprintf('The HTTP status code "%s" is not valid.', $httpStatusCode));
4✔
565
                                }
566
                            }
567

UNCOV
568
                            return $exceptionToStatus;
1✔
UNCOV
569
                        })
21✔
UNCOV
570
                    ->end()
21✔
UNCOV
571
                ->end()
21✔
UNCOV
572
            ->end();
21✔
573
    }
574

575
    private function addFormatSection(ArrayNodeDefinition $rootNode, string $key, array $defaultValue): void
576
    {
UNCOV
577
        $rootNode
21✔
UNCOV
578
            ->children()
21✔
UNCOV
579
                ->arrayNode($key)
21✔
UNCOV
580
                    ->defaultValue($defaultValue)
21✔
UNCOV
581
                    ->info('The list of enabled formats. The first one will be the default.')
21✔
UNCOV
582
                    ->normalizeKeys(false)
21✔
UNCOV
583
                    ->useAttributeAsKey('format')
21✔
UNCOV
584
                    ->beforeNormalization()
21✔
UNCOV
585
                        ->ifArray()
21✔
UNCOV
586
                        ->then(static function ($v) {
21✔
UNCOV
587
                            foreach ($v as $format => $value) {
1✔
UNCOV
588
                                if (isset($value['mime_types'])) {
1✔
589
                                    continue;
×
590
                                }
591

UNCOV
592
                                $v[$format] = ['mime_types' => $value];
1✔
593
                            }
594

UNCOV
595
                            return $v;
1✔
UNCOV
596
                        })
21✔
UNCOV
597
                    ->end()
21✔
UNCOV
598
                    ->prototype('array')
21✔
UNCOV
599
                        ->children()
21✔
UNCOV
600
                            ->arrayNode('mime_types')->prototype('scalar')->end()->end()
21✔
UNCOV
601
                        ->end()
21✔
UNCOV
602
                    ->end()
21✔
UNCOV
603
                ->end()
21✔
UNCOV
604
            ->end();
21✔
605
    }
606

607
    private function addDefaultsSection(ArrayNodeDefinition $rootNode): void
608
    {
UNCOV
609
        $nameConverter = new CamelCaseToSnakeCaseNameConverter();
21✔
UNCOV
610
        $defaultsNode = $rootNode->children()->arrayNode('defaults');
21✔
611

UNCOV
612
        $defaultsNode
21✔
UNCOV
613
            ->ignoreExtraKeys(false)
21✔
UNCOV
614
            ->beforeNormalization()
21✔
UNCOV
615
            ->always(static function (array $defaults) use ($nameConverter): array {
21✔
UNCOV
616
                $normalizedDefaults = [];
1✔
UNCOV
617
                foreach ($defaults as $option => $value) {
1✔
UNCOV
618
                    $option = $nameConverter->normalize($option);
1✔
UNCOV
619
                    $normalizedDefaults[$option] = $value;
1✔
620
                }
621

UNCOV
622
                return $normalizedDefaults;
1✔
UNCOV
623
            });
21✔
624

UNCOV
625
        $this->defineDefault($defaultsNode, new \ReflectionClass(ApiResource::class), $nameConverter);
21✔
UNCOV
626
        $this->defineDefault($defaultsNode, new \ReflectionClass(Put::class), $nameConverter);
21✔
UNCOV
627
        $this->defineDefault($defaultsNode, new \ReflectionClass(Post::class), $nameConverter);
21✔
628
    }
629

630
    private function addMakerSection(ArrayNodeDefinition $rootNode): void
631
    {
UNCOV
632
        $rootNode
21✔
UNCOV
633
            ->children()
21✔
UNCOV
634
                ->arrayNode('maker')
21✔
UNCOV
635
                    ->{class_exists(MakerBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
21✔
UNCOV
636
                ->end()
21✔
UNCOV
637
            ->end();
21✔
638
    }
639

640
    private function defineDefault(ArrayNodeDefinition $defaultsNode, \ReflectionClass $reflectionClass, CamelCaseToSnakeCaseNameConverter $nameConverter): void
641
    {
UNCOV
642
        foreach ($reflectionClass->getConstructor()->getParameters() as $parameter) {
21✔
UNCOV
643
            $defaultsNode->children()->variableNode($nameConverter->normalize($parameter->getName()));
21✔
644
        }
645
    }
646
}
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