Skip to content

Commit

Permalink
Added strict_mode for REST APIs (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p authored Sep 3, 2024
1 parent 171a913 commit 397d3b5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,11 @@ parameters:
count: 1
path: src/bundle/DependencyInjection/Compiler/ValueObjectVisitorPass.php

-
message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\Configuration\\:\\:addRestRootResourcesSection\\(\\) has no return type specified\\.$#"
count: 1
path: src/bundle/DependencyInjection/Configuration.php

-
message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\Configuration\\:\\:addRestRootResourcesSection\\(\\) has parameter \\$rootNode with no type specified\\.$#"
count: 1
path: src/bundle/DependencyInjection/Configuration.php

-
message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\IbexaRestExtension\\:\\:load\\(\\) has no return type specified\\.$#"
count: 1
path: src/bundle/DependencyInjection/IbexaRestExtension.php

-
message: "#^Method Ibexa\\\\Bundle\\\\Rest\\\\DependencyInjection\\\\IbexaRestExtension\\:\\:prepend\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
15 changes: 12 additions & 3 deletions src/bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@

class Configuration extends SiteAccessConfiguration
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder(IbexaRestExtension::EXTENSION_NAME);

$this->addRestRootResourcesSection($treeBuilder->getRootNode());
$rootNode = $treeBuilder->getRootNode();
$rootNode
->children()
->booleanNode('strict_mode')
->defaultValue('%kernel.debug%')
->info('Throw exceptions for missing normalizers.')
->end()
->end();

$this->addRestRootResourcesSection($rootNode);

return $treeBuilder;
}

public function addRestRootResourcesSection($rootNode)
private function addRestRootResourcesSection($rootNode): void
{
$systemNode = $this->generateScopeBaseNode($rootNode);
$systemNode
Expand Down
13 changes: 6 additions & 7 deletions src/bundle/DependencyInjection/IbexaRestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
use Symfony\Component\Yaml\Yaml;

/**
* This is the class that loads and manages your bundle configuration.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class IbexaRestExtension extends Extension implements PrependExtensionInterface
class IbexaRestExtension extends ConfigurableExtension implements PrependExtensionInterface
{
public const EXTENSION_NAME = 'ibexa_rest';

Expand All @@ -30,12 +30,11 @@ public function getAlias(): string
}

/**
* {@inheritdoc}
* @param array<string, mixed> $mergedConfig
*/
public function load(array $configs, ContainerBuilder $container)
protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('ibexa.rest.strict_mode', $mergedConfig['strict_mode']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
Expand All @@ -45,7 +44,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('default_settings.yml');

$processor = new ConfigurationProcessor($container, 'ibexa.site_access.config');
$processor->mapConfigArray('rest_root_resources', $config);
$processor->mapConfigArray('rest_root_resources', $mergedConfig);
}

public function prepend(ContainerBuilder $container)
Expand Down
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ services:
arguments:
$normalizer: '@ibexa.rest.serializer'
$logger: '@logger'
$strictMode: '%ibexa.rest.strict_mode%'
tags:
- { name: monolog.logger, channel: ibexa.rest }

Expand All @@ -357,6 +358,7 @@ services:
arguments:
$normalizer: '@ibexa.rest.serializer'
$logger: '@logger'
$strictMode: '%ibexa.rest.strict_mode%'
tags:
- { name: monolog.logger, channel: ibexa.rest }

Expand Down
9 changes: 8 additions & 1 deletion src/lib/Output/Generator/Json/FieldTypeHashGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ class FieldTypeHashGenerator implements LoggerAwareInterface

private NormalizerInterface $normalizer;

private bool $strictMode;

public function __construct(
NormalizerInterface $normalizer,
?LoggerInterface $logger = null
?LoggerInterface $logger = null,
bool $strictMode = false
) {
$this->normalizer = $normalizer;
$this->logger = $logger ?? new NullLogger();
$this->strictMode = $strictMode;
}

/**
Expand Down Expand Up @@ -152,6 +156,9 @@ private function generateObjectValue($parent, object $value)
try {
$value = $this->normalizer->normalize($value, 'json', ['parent' => $parent]);
} catch (ExceptionInterface $e) {
if ($this->strictMode) {
throw $e;
}
$message = sprintf(
'Unable to normalize value for type "%s". %s. '
. 'Ensure that a normalizer is registered with tag: "%s".',
Expand Down
9 changes: 8 additions & 1 deletion src/lib/Output/Generator/Xml/FieldTypeHashGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ class FieldTypeHashGenerator implements LoggerAwareInterface

private NormalizerInterface $normalizer;

private bool $strictMode;

public function __construct(
NormalizerInterface $normalizer,
?LoggerInterface $logger = null
?LoggerInterface $logger = null,
bool $strictMode = false
) {
$this->normalizer = $normalizer;
$this->logger = $logger ?? new NullLogger();
$this->strictMode = $strictMode;
}

/**
Expand Down Expand Up @@ -243,6 +247,9 @@ private function generateObjectValue(object $value, \XmlWriter $writer, ?string
try {
$value = $this->normalizer->normalize($value, 'xml');
} catch (ExceptionInterface $e) {
if ($this->strictMode) {
throw $e;
}
$message = sprintf(
'Unable to normalize value for type "%s". %s. '
. 'Ensure that a normalizer is registered with tag: "%s".',
Expand Down

0 comments on commit 397d3b5

Please sign in to comment.