Skip to content

Commit 17c6b58

Browse files
authored
fix(state): log on missing provider (#6519)
1 parent 69c8825 commit 17c6b58

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/State/Provider/ReadProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ApiPlatform\State\UriVariablesResolverTrait;
2525
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
2626
use ApiPlatform\State\Util\RequestParser;
27+
use Psr\Log\LoggerInterface;
2728
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2829

2930
/**
@@ -40,6 +41,7 @@ final class ReadProvider implements ProviderInterface
4041
public function __construct(
4142
private readonly ProviderInterface $provider,
4243
private readonly LegacySerializerContextBuilderInterface|SerializerContextBuilderInterface|null $serializerContextBuilder = null,
44+
private readonly ?LoggerInterface $logger = null,
4345
) {
4446
}
4547

@@ -63,10 +65,12 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6365
$context['filters'] = $filters;
6466
}
6567

68+
$resourceClass = $operation->getClass();
69+
6670
if ($this->serializerContextBuilder && $request) {
6771
// Builtin data providers are able to use the serialization context to automatically add join clauses
6872
$context += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, [
69-
'resource_class' => $operation->getClass(),
73+
'resource_class' => $resourceClass,
7074
'operation' => $operation,
7175
]);
7276
$request->attributes->set('_api_normalization_context', $normalizationContext);
@@ -75,6 +79,8 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
7579
try {
7680
$data = $this->provider->provide($operation, $uriVariables, $context);
7781
} catch (ProviderNotFoundException $e) {
82+
// In case the dev just forgot to implement it
83+
$this->logger?->debug('No provider registered for {resource_class}', ['resource_class' => $resourceClass]);
7884
$data = null;
7985
}
8086

@@ -85,7 +91,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
8591
|| ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
8692
)
8793
) {
88-
throw new NotFoundHttpException('Not Found');
94+
throw new NotFoundHttpException('Not Found', $e ?? null);
8995
}
9096

9197
$request?->attributes->set('data', $data);

src/Symfony/Bundle/Resources/config/symfony/events.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<service id="api_platform.state_provider.read" class="ApiPlatform\State\Provider\ReadProvider">
2222
<argument type="service" id="api_platform.state_provider.locator" />
2323
<argument type="service" id="api_platform.serializer.context_builder" />
24+
<argument key="$logger" type="service" id="logger" on-invalid="null" />
2425
</service>
2526

2627
<!-- kernel.request priority must be < 8 to be executed after the Firewall -->

0 commit comments

Comments
 (0)