Skip to content

Commit 6a628c1

Browse files
fix(metadata): fix POST on subresource (#5734)
1 parent cbc21b0 commit 6a628c1

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

CreateProvider.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use ApiPlatform\Metadata\Link;
2020
use ApiPlatform\Metadata\Operation;
2121
use ApiPlatform\Metadata\Post;
22+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2223
use Symfony\Component\PropertyAccess\PropertyAccess;
2324
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2425

@@ -59,12 +60,17 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5960
}
6061

6162
$relation = $this->decorated->provide(new Get(uriVariables: $relationUriVariables, class: $relationClass), $uriVariables);
63+
if (!$relation) {
64+
throw new NotFoundHttpException('Not Found');
65+
}
66+
6267
try {
6368
$resource = new ($operation->getClass());
6469
} catch (\Throwable $e) {
6570
throw new RuntimeException(sprintf('An error occurred while trying to create an instance of the "%s" resource. Consider writing your own "%s" implementation and setting it as `provider` on your operation instead.', $operation->getClass(), ProviderInterface::class), 0, $e);
6671
}
67-
$this->propertyAccessor->setValue($resource, $key, $relation);
72+
$property = $operationUriVariables[$key]->getToProperty() ?? $key;
73+
$this->propertyAccessor->setValue($resource, $property, $relation);
6874

6975
return $resource;
7076
}

0 commit comments

Comments
 (0)