Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 502234c

Browse files
Sarahshrsoyuka
authored andcommitted
fix: allowed composite identifiers with differents types
fixes #5396
1 parent 9116f15 commit 502234c

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

features/main/composite.feature

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,8 @@ Feature: Retrieve data with Composite identifiers
132132
Then the response status code should be 200
133133
And the response should be in JSON
134134
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
135+
136+
Scenario: Get identifiers with different types
137+
Given there are Composite identifier objects
138+
When I send a "GET" request to "/composite_key_with_different_types/id=82133;verificationKey=7d75af772e637e45c36d041696e1128d"
139+
Then the response status code should be 200

src/Api/UriVariablesConverter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public function convert(array $uriVariables, string $class, array $context = [])
4444

4545
foreach ($uriVariables as $parameterName => $value) {
4646
$uriVariableDefinition = $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();
47-
if ([] === $types = $this->getIdentifierTypes($uriVariableDefinition->getFromClass() ?? $class, $uriVariableDefinition->getIdentifiers() ?? [$parameterName])) {
47+
48+
$identifierTypes = $this->getIdentifierTypes($uriVariableDefinition->getFromClass() ?? $class, $uriVariableDefinition->getIdentifiers() ?? [$parameterName]);
49+
if (!($types = $identifierTypes[$parameterName] ?? false)) {
4850
continue;
4951
}
5052

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5396;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\Operation;
19+
20+
#[ApiResource(provider: [CompositeKeyWithDifferentType::class, 'provide'])]
21+
class CompositeKeyWithDifferentType
22+
{
23+
#[ApiProperty(identifier: true)]
24+
public ?int $id;
25+
26+
#[ApiProperty(identifier: true)]
27+
public ?string $verificationKey;
28+
29+
public static function provide(Operation $operation, array $uriVariables = [], array $context = []): array
30+
{
31+
return $context;
32+
}
33+
}

0 commit comments

Comments
 (0)