Skip to content
Open
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
11 changes: 10 additions & 1 deletion features/main/attribute_resource.feature
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,17 @@ Feature: Resource attributes

Scenario: Uri variables with Post operation
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/post_with_uri_variables/{id}" with body:
And I send a "POST" request to "/post_with_uri_variables_and_no_provider/{id}" with body:
"""
{}
"""
Then the response status code should be 201

Scenario: Throw validation exception in a provider
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/post_with_uri_variables/{id}" with body:
"""
{}
"""
Then the response status code should be 422

5 changes: 5 additions & 0 deletions features/main/composite.feature
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,8 @@ Feature: Retrieve data with Composite identifiers
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"

Scenario: Get identifiers with different types
Given there are Composite identifier objects
When I send a "GET" request to "/composite_key_with_different_types/id=82133;verificationKey=7d75af772e637e45c36d041696e1128d"
Then the response status code should be 200
6 changes: 4 additions & 2 deletions src/Api/UriVariablesConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public function convert(array $uriVariables, string $class, array $context = [])

foreach ($uriVariables as $parameterName => $value) {
$uriVariableDefinition = $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();
if ([] === $types = $this->getIdentifierTypes($uriVariableDefinition->getFromClass() ?? $class, $uriVariableDefinition->getIdentifiers() ?? [$parameterName])) {

$identifierTypes = $this->getIdentifierTypes($uriVariableDefinition->getFromClass() ?? $class, $uriVariableDefinition->getIdentifiers() ?? [$parameterName]);
if (!($types = $identifierTypes[$parameterName] ?? false)) {
continue;
}

Expand All @@ -71,7 +73,7 @@ private function getIdentifierTypes(string $resourceClass, array $properties): a
foreach ($properties as $property) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
foreach ($propertyMetadata->getBuiltinTypes() as $type) {
$types[] = Type::BUILTIN_TYPE_OBJECT === ($builtinType = $type->getBuiltinType()) ? $type->getClassName() : $builtinType;
$types[$property][] = Type::BUILTIN_TYPE_OBJECT === ($builtinType = $type->getBuiltinType()) ? $type->getClassName() : $builtinType;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/State/CallableProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace ApiPlatform\State;

use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Exception\ProviderNotFoundException;
use Psr\Container\ContainerInterface;

final class CallableProvider implements ProviderInterface
Expand All @@ -34,7 +34,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c

if (\is_string($provider)) {
if (!$this->locator->has($provider)) {
throw new RuntimeException(sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
throw new ProviderNotFoundException(sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
}

/** @var ProviderInterface $providerInstance */
Expand All @@ -43,6 +43,6 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
return $providerInstance->provide($operation, $uriVariables, $context);
}

throw new RuntimeException(sprintf('Provider not found on operation "%s"', $operation->getName()));
throw new ProviderNotFoundException(sprintf('Provider not found on operation "%s"', $operation->getName()));
}
}
20 changes: 20 additions & 0 deletions src/State/Exception/ProviderNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\State\Exception;

use ApiPlatform\Metadata\Exception\RuntimeException;

final class ProviderNotFoundException extends RuntimeException
{
}
4 changes: 2 additions & 2 deletions src/Symfony/EventListener/ReadListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
use ApiPlatform\Api\UriVariablesConverterInterface;
use ApiPlatform\Exception\InvalidIdentifierException;
use ApiPlatform\Exception\InvalidUriVariableException;
use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
use ApiPlatform\State\Exception\ProviderNotFoundException;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\UriVariablesResolverTrait;
use ApiPlatform\Util\CloneTrait;
Expand Down Expand Up @@ -93,7 +93,7 @@ public function onKernelRequest(RequestEvent $event): void
$data = $this->provider->provide($operation, $uriVariables, $context);
} catch (InvalidIdentifierException|InvalidUriVariableException $e) {
throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
} catch (RuntimeException $e) {
} catch (ProviderNotFoundException $e) {
$data = null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5396;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Operation;

#[ApiResource(provider: [CompositeKeyWithDifferentType::class, 'provide'])]
class CompositeKeyWithDifferentType
{
#[ApiProperty(identifier: true)]
public ?int $id;

#[ApiProperty(identifier: true)]
public ?string $verificationKey;

public static function provide(Operation $operation, array $uriVariables = [], array $context = []): array
{
return $context;
}
}
10 changes: 9 additions & 1 deletion tests/Fixtures/TestBundle/ApiResource/PostWithUriVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use ApiPlatform\Metadata\NotExposed;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Symfony\Validator\Exception\ValidationException as ExceptionValidationException;
use Symfony\Component\Validator\ConstraintViolationList;

#[NotExposed(uriTemplate: '/post_with_uri_variables/{id}')]
#[Post(uriTemplate: '/post_with_uri_variables/{id}', uriVariables: ['id'], processor: [PostWithUriVariables::class, 'process'])]
#[Post(uriTemplate: '/post_with_uri_variables_and_no_provider/{id}', uriVariables: ['id'], processor: [PostWithUriVariables::class, 'process'])]
#[Post(uriTemplate: '/post_with_uri_variables/{id}', uriVariables: ['id'], provider: [PostWithUriVariables::class, 'provide'])]
final class PostWithUriVariables
{
public function __construct(public readonly ?int $id = null)
Expand All @@ -29,4 +32,9 @@ public static function process(mixed $data, Operation $operation, array $uriVari
{
return new self(id: 1);
}

public static function provide(Operation $operation, array $uriVariables = [], array $context = []): void
{
throw new ExceptionValidationException(new ConstraintViolationList());
}
}