Skip to content

Commit

Permalink
feat(metadata): throwOnNotFound option
Browse files Browse the repository at this point in the history
fixes #6014
  • Loading branch information
soyuka committed Dec 7, 2023
1 parent d85ed0c commit 741f2c5
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 8 deletions.
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 @@ public function __construct(
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 @@ public function withStateOptions(?OptionsInterface $stateOptions): static
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

0 comments on commit 741f2c5

Please sign in to comment.