Skip to content

Commit

Permalink
fix: allowed composite identifiers with differents types
Browse files Browse the repository at this point in the history
fixes #5396
  • Loading branch information
Sarahshr authored and soyuka committed May 23, 2023
1 parent 9116f15 commit 502234c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
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
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 <dunglas@gmail.com>
*
* 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;
}
}

0 comments on commit 502234c

Please sign in to comment.