Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/composite identifiers 5396 #5589

Merged
merged 2 commits into from
May 22, 2023
Merged
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
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the issue, this PR makes an assumption that the parameterName is always in sync with properties, which is not the case as shown here https://github.com/api-platform/core/blob/main/tests/Fixtures/TestBundle/Resources/config/api_resources_odm/resources.xml#L14

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad.

}
}

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;
}
}