Skip to content
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

feat(metadata): throwOnNotFound option #6027

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions src/Metadata/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ public function __construct(
$provider = null,
$processor = null,
protected ?OptionsInterface $stateOptions = null,
protected ?bool $throwOnNotFound = null,
protected array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -998,6 +999,7 @@ class: $class,
provider: $provider,
processor: $processor,
stateOptions: $stateOptions,
throwOnNotFound: $throwOnNotFound,
extraProperties: $extraProperties
);

Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __construct(
$provider = null,
$processor = null,
OptionsInterface $stateOptions = null,
bool $throwOnNotFound = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -167,6 +168,7 @@ class: $class,
processor: $processor,
extraProperties: $extraProperties,
collectDenormalizationErrors: $collectDenormalizationErrors,
throwOnNotFound: $throwOnNotFound,
stateOptions: $stateOptions,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __construct(
$provider = null,
$processor = null,
OptionsInterface $stateOptions = null,
bool $throwOnNotFound = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -166,6 +167,7 @@ class: $class,
provider: $provider,
processor: $processor,
stateOptions: $stateOptions,
throwOnNotFound: $throwOnNotFound,
extraProperties: $extraProperties,
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Metadata/GetCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function __construct(
$processor = null,
OptionsInterface $stateOptions = null,
array $extraProperties = [],
bool $throwOnNotFound = null,
private ?string $itemUriTemplate = null,
) {
parent::__construct(
Expand Down Expand Up @@ -166,8 +167,9 @@ class: $class,
name: $name,
provider: $provider,
processor: $processor,
extraProperties: $extraProperties,
throwOnNotFound: $throwOnNotFound,
stateOptions: $stateOptions,
extraProperties: $extraProperties,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/HttpOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public function __construct(
$provider = null,
$processor = null,
OptionsInterface $stateOptions = null,
bool $throwOnNotFound = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -244,6 +245,7 @@ class: $class,
provider: $provider,
processor: $processor,
stateOptions: $stateOptions,
throwOnNotFound: $throwOnNotFound,
extraProperties: $extraProperties
);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Metadata/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
protected $provider = null,
protected $processor = null,
protected ?OptionsInterface $stateOptions = null,
protected ?bool $throwOnNotFound = null,
protected array $extraProperties = []
) {
}
Expand Down Expand Up @@ -566,6 +567,19 @@
return $self;
}

public function getThrowOnNotFound(): ?bool
{
return $this->throwOnNotFound;
}

public function withThrowOnNotFound(bool $throwOnNotFound): bool
{
$self = clone $this;
$self->throwOnNotFound = $throwOnNotFound;

return $self;

Check failure on line 580 in src/Metadata/Metadata.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2)

Method ApiPlatform\Metadata\Metadata::withThrowOnNotFound() should return bool but returns $this(ApiPlatform\Metadata\Metadata).
}

public function getExtraProperties(): ?array
{
return $this->extraProperties;
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ public function __construct(
protected $provider = null,
protected $processor = null,
protected ?OptionsInterface $stateOptions = null,
protected ?bool $throwOnNotFound = null,
protected array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -839,6 +840,7 @@ class: $class,
provider: $provider,
processor: $processor,
stateOptions: $stateOptions,
throwOnNotFound: $throwOnNotFound,
extraProperties: $extraProperties,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function __construct(
string $name = null,
$provider = null,
$processor = null,
bool $throwOnNotFound = null,
OptionsInterface $stateOptions = null,
array $extraProperties = [],
) {
Expand Down Expand Up @@ -167,6 +168,7 @@ class: $class,
provider: $provider,
processor: $processor,
stateOptions: $stateOptions,
throwOnNotFound: $throwOnNotFound,
extraProperties: $extraProperties
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __construct(
$provider = null,
$processor = null,
OptionsInterface $stateOptions = null,
bool $throwOnNotFound = null,
array $extraProperties = [],
private ?string $itemUriTemplate = null
) {
Expand Down Expand Up @@ -168,6 +169,7 @@ class: $class,
provider: $provider,
processor: $processor,
stateOptions: $stateOptions,
throwOnNotFound: $throwOnNotFound,
extraProperties: $extraProperties
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Put.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __construct(
$provider = null,
$processor = null,
OptionsInterface $stateOptions = null,
bool $throwOnNotFound = null,
array $extraProperties = [],
private ?bool $allowCreate = null,
) {
Expand Down Expand Up @@ -168,6 +169,7 @@ class: $class,
provider: $provider,
processor: $processor,
stateOptions: $stateOptions,
throwOnNotFound: $throwOnNotFound,
extraProperties: $extraProperties
);
}
Expand Down
8 changes: 1 addition & 7 deletions src/State/Provider/ReadProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
$data = null;
}

if (
null === $data
&& 'POST' !== $operation->getMethod()
&& ('PUT' !== $operation->getMethod()
|| ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
)
) {
if (null === $data && $operation->getThrowOnNotFound()) {
throw new NotFoundHttpException('Not Found');
}

Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ApiPlatform\Metadata\Error;
use ApiPlatform\Metadata\Exception\RuntimeException;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
Expand Down Expand Up @@ -79,6 +80,15 @@ public function __invoke(Request $request): Response
$operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true));
}

if (null === $operation->getThrowOnNotFound() && $operation instanceof HttpOperation) {
$operation->withThrowOnNotFound(
'POST' !== $operation->getMethod()
&& ('PUT' !== $operation->getMethod()
|| ($operation instanceof Put && !($operation->getAllowCreate() ?? false))
)
);
}

$body = $this->provider->provide($operation, $uriVariables, $context);

// The provider can change the Operation, extract it again from the Request attributes
Expand Down
Loading