Skip to content
Snippets Groups Projects

Issue #3458259 by jsacksick: Ensure the DocumentResolver does not try to resolve GET requests.

Merged Issue #3458259 by jsacksick: Ensure the DocumentResolver does not try to resolve GET requests.
@@ -13,6 +13,7 @@ use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
// The ArgumentValueResolverInterface is deprecated since Symfony 6.2 and
// removed from Symfony 7. Hence, below workaround to run PHPUnit tests against
// Drupal 9, 10 and 11.
// @todo Remove when all supported versions require Symfony 7.
if (!interface_exists(ValueResolverInterface::class)) {
class_alias('\Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface', ValueResolverInterface::class);
}
@@ -24,31 +25,53 @@ if (!interface_exists(ValueResolverInterface::class)) {
*/
final class DocumentResolver implements ValueResolverInterface {
public function __construct(
protected DocumentExtractor $documentExtractor,
) {}
/**
* The document extractor.
* Backwards-compatibility layer for Symfony < 7.
*
* @todo Remove when all supported versions of Drupal require Symfony 7.
*
* @param Request $request
* Request.
* @param ArgumentMetadata $argument
* Argument metadata.
*
* @var \Drupal\jsonapi_resources\Unstable\DocumentExtractor
* @return bool
* Flag indicating supported status.
*/
protected $documentExtractor;
public function supports(Request $request, ArgumentMetadata $argument): bool {
return $this->shouldResolve($request, $argument);
}
/**
* Constructs a JSON:API document argument resolver.
*
* @param \Drupal\jsonapi_resources\Unstable\DocumentExtractor $document_extractor
* The document extractor.
* {@inheritdoc}
*/
public function __construct(DocumentExtractor $document_extractor) {
$this->documentExtractor = $document_extractor;
public function resolve(Request $request, ArgumentMetadata $argument): iterable {
if (!$this->shouldResolve($request, $argument)) {
return [];
}
yield $this->documentExtractor->getDocument($request);
}
/**
* {@inheritdoc}
* Gets whether the given request should be resolved.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
* @param \Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata $argument
* The argument.
*
* @return bool
* Whether the given request should be resolved.
*/
public function supports(Request $request, ArgumentMetadata $argument): bool {
private function shouldResolve(Request $request, ArgumentMetadata $argument): bool {
$supported_method = in_array($request->getMethod(), [
'POST',
'PATCH',
]);
], TRUE);
$is_delete = $request->isMethod('DELETE');
$is_relationship = $request->attributes->has('_jsonapi_relationship_field_name');
$supported_method = $supported_method || ($is_delete && $is_relationship);
@@ -57,11 +80,4 @@ final class DocumentResolver implements ValueResolverInterface {
return $supported_method && $supported_format && $correct_type;
}
/**
* {@inheritdoc}
*/
public function resolve(Request $request, ArgumentMetadata $argument): iterable {
yield $this->documentExtractor->getDocument($request);
}
}
Loading