Skip to content

[Config][FrameworkBundle] Generate JSON schema for config #59620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
* Add `RateLimiterFactoryInterface` as an alias of the `limiter` service
* Add `framework.validation.disable_translation` option
* Add support for signal plain name in the `messenger.stop_worker_on_signals` configuration
* Generate JSON schema for YAML configuration

7.2
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;

use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\JsonSchemaGenerator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Generate config json schema.
*/
final readonly class ConfigSchemaCacheWarmer implements CacheWarmerInterface
{
public function __construct(
private KernelInterface $kernel,
private ?LoggerInterface $logger = null,
) {
}

public function warmUp(string $cacheDir, ?string $buildDir = null): array

Check failure on line 38 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingOverrideAttribute

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:38:5: MissingOverrideAttribute: Method Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer::warmup should have the "Override" attribute (see https://psalm.dev/358)

Check failure on line 38 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingOverrideAttribute

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:38:5: MissingOverrideAttribute: Method Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer::warmup should have the "Override" attribute (see https://psalm.dev/358)
{
if (!$buildDir || !$this->kernel->isDebug() || !class_exists(JsonSchemaGenerator::class)) {
return [];
}

$generator = new JsonSchemaGenerator(\dirname($this->kernel->getBuildDir()).'/config.schema.json');

if ($this->kernel instanceof Kernel) {
/** @var ContainerBuilder $container */
$container = \Closure::bind(function (Kernel $kernel) {
$containerBuilder = $kernel->getContainerBuilder();

Check failure on line 49 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleMethod

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:49:46: InaccessibleMethod: Cannot access protected method Symfony\Component\HttpKernel\Kernel::getContainerBuilder from context Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer (see https://psalm.dev/003)

Check failure on line 49 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleMethod

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:49:46: InaccessibleMethod: Cannot access protected method Symfony\Component\HttpKernel\Kernel::getContainerBuilder from context Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer (see https://psalm.dev/003)
$kernel->prepareContainer($containerBuilder);

Check failure on line 50 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleMethod

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:50:26: InaccessibleMethod: Cannot access protected method Symfony\Component\HttpKernel\Kernel::prepareContainer from context Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer (see https://psalm.dev/003)

Check failure on line 50 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleMethod

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:50:26: InaccessibleMethod: Cannot access protected method Symfony\Component\HttpKernel\Kernel::prepareContainer from context Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer (see https://psalm.dev/003)

return $containerBuilder;
}, null, $this->kernel)($this->kernel);

$extensions = $container->getExtensions();
} else {
$extensions = [];
foreach ($this->kernel->getBundles() as $bundle) {
$extension = $bundle->getContainerExtension();
if (null !== $extension) {
$extensions[] = $extension;
}
}
}

$tree = new ArrayNode($this->kernel->getEnvironment());
foreach ($extensions as $extension) {
try {
$configuration = null;
if ($extension instanceof ConfigurationInterface) {
$configuration = $extension;
} elseif ($extension instanceof ConfigurationExtensionInterface) {
$container = $this->kernel->getContainer();
$configuration = $extension->getConfiguration([], new ContainerBuilder($container instanceof Container ? new ContainerBag($container) : new ParameterBag()));
}

if (!$extensionConfigNode = $configuration?->getConfigTreeBuilder()->buildTree()) {
continue;
}

$tree->addChild($extensionConfigNode);
} catch (\Exception $e) {
$this->logger?->warning('Failed to generate JSON schema for extension {extensionClass}: '.$e->getMessage(), ['exception' => $e, 'extensionClass' => $extension::class]);
}
}

try {
$generator->merge($tree, (object) [
'description' => 'Symfony configuration',
'properties' => (object) [
'when@'.$tree->getName() => ['$ref' => '#/definitions/'.$tree->getName()],
],
'patternProperties' => (object) [
'when@[a-zA-Z0-9]+' => ['$ref' => '#/'],
],
]);
} catch (\Exception $e) {
$this->logger?->warning('Failed to generate JSON schema for the configuration: '.$e->getMessage(), ['exception' => $e]);
}

// No need to preload anything
return [];
}

public function isOptional(): bool

Check failure on line 105 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingOverrideAttribute

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:105:5: MissingOverrideAttribute: Method Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer::isoptional should have the "Override" attribute (see https://psalm.dev/358)

Check failure on line 105 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingOverrideAttribute

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:105:5: MissingOverrideAttribute: Method Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer::isoptional should have the "Override" attribute (see https://psalm.dev/358)
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Clock\ClockInterface as PsrClockInterface;
use Psr\EventDispatcher\EventDispatcherInterface as PsrEventDispatcherInterface;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigBuilderCacheWarmer;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer;
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Clock\ClockInterface;
Expand Down Expand Up @@ -233,6 +234,10 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
->args([service(KernelInterface::class), service('logger')->nullOnInvalid()])
->tag('kernel.cache_warmer')

->set('config_schema.warmer', ConfigSchemaCacheWarmer::class)
->args([service(KernelInterface::class), service('logger')->nullOnInvalid()])
->tag('kernel.cache_warmer')

->set('clock', Clock::class)
->alias(ClockInterface::class, 'clock')
->alias(PsrClockInterface::class, 'clock')
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add `ExprBuilder::ifFalse()`
* Add support for info on `ArrayNodeDefinition::canBeEnabled()` and `ArrayNodeDefinition::canBeDisabled()`
* Add `JsonSchemaGenerator`

7.2
---
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Config/Definition/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ public function addEquivalentValue(mixed $originalValue, mixed $equivalentValue)
$this->equivalentValues[] = [$originalValue, $equivalentValue];
}

/**
* @internal
*/
public function getEquivalentValues(): array
{
return $this->equivalentValues;
}

/**
* Set this node as required.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Config/Definition/BooleanNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public function __construct(
parent::__construct($name, $parent, $pathSeparator);
}

/**
* @internal
*/
public function isNullable(): bool
{
return $this->nullable;
}

protected function validateType(mixed $value): void
{
if (!\is_bool($value)) {
Expand Down
Loading
Loading