Skip to content
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
3 changes: 3 additions & 0 deletions src/JsonApi/Filter/SparseFieldsetParameterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\State\ParameterProviderInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

final readonly class SparseFieldsetParameterProvider implements ParameterProviderInterface
Expand All @@ -31,6 +32,7 @@ public function __construct(
private PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
private PropertyMetadataFactoryInterface $propertyMetadataFactory,
private ResourceLinkageResolver $resourceLinkageResolver,
private ?NameConverterInterface $nameConverter = null,
) {
}

Expand Down Expand Up @@ -68,6 +70,7 @@ public function provide(Parameter $parameter, array $parameters = [], array $con
}

foreach (explode(',', $fields) as $f) {
$f = $this->nameConverter?->denormalize($f) ?? $f;
if (\in_array($f, $resourceAllowedProperties, true)) {
$p[] = $f;
}
Expand Down
24 changes: 23 additions & 1 deletion src/JsonApi/Tests/Filter/SparseFieldsetParameterProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\TypeInfo\Type;

Expand Down Expand Up @@ -90,7 +92,26 @@ public function testFieldsKeyMatchingThePropertyNameStillWorks(): void
);
}

private function createProvider(string $authorShortName): SparseFieldsetParameterProvider
public function testFieldsAreDenormalizedWithTheNameConverter(): void
{
$operation = new Get(class: SparseFieldsetIncludeArticleFixture::class, shortName: 'SparseFieldsetIncludeArticle');

$provider = $this->createProvider(authorShortName: 'SparseFieldsetIncludeAuthor', nameConverter: new CamelCaseToSnakeCaseNameConverter());

$parameter = (new QueryParameter(key: 'fields'))
->withProperties(['title', 'artistCredit'])
->setValue(['SparseFieldsetIncludeArticle' => 'title,artist_credit,unknown_field']);

$result = $provider->provide($parameter, [], ['operation' => $operation]);

$this->assertNotNull($result);
$this->assertSame(
['title', 'artistCredit'],
$result->getNormalizationContext()[AbstractNormalizer::ATTRIBUTES] ?? null,
);
}

private function createProvider(string $authorShortName, ?NameConverterInterface $nameConverter = null): SparseFieldsetParameterProvider
{
$articleClass = SparseFieldsetIncludeArticleFixture::class;
$authorClass = SparseFieldsetIncludeAuthorFixture::class;
Expand Down Expand Up @@ -144,6 +165,7 @@ private function createProvider(string $authorShortName): SparseFieldsetParamete
$propertyNameCollectionFactory,
$propertyMetadataFactory,
new ResourceLinkageResolver($resourceClassResolver),
$nameConverter,
);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bundle/Resources/config/state/jsonapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
service('api_platform.metadata.property.name_collection_factory'),
service('api_platform.metadata.property.metadata_factory'),
service('api_platform.jsonapi.resource_linkage_resolver'),
service('api_platform.name_converter')->ignoreOnInvalid(),
])
->tag('api_platform.parameter_provider', ['key' => SparseFieldsetParameterProvider::class]);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\JsonApi;

use ApiPlatform\JsonApi\Filter\SparseFieldset;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\QueryParameter;

#[ApiResource(
shortName: 'SparseFieldsetNameConverted',
operations: [
new Get(
uriTemplate: '/sparse_fieldset_name_converted/{id}',
uriVariables: ['id'],
provider: [self::class, 'provide'],
),
],
parameters: ['fields' => new QueryParameter(filter: SparseFieldset::class)],
)]
final class SparseFieldsetNameConverted
{
public function __construct(
#[ApiProperty(identifier: true)]
public int $id,
public string $title,
public string $nameConverted,
) {
}

public static function provide(Operation $operation, array $uriVariables = []): self
{
$id = (int) ($uriVariables['id'] ?? 1);

return new self($id, 'Title #'.$id, 'Converted #'.$id);
}
}
45 changes: 45 additions & 0 deletions tests/Functional/JsonApi/SparseFieldsetNameConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Functional\JsonApi;

use ApiPlatform\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonApi\SparseFieldsetNameConverted;
use ApiPlatform\Tests\SetupClassResourcesTrait;

final class SparseFieldsetNameConverterTest extends ApiTestCase
{
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

public static function getResources(): array
{
return [SparseFieldsetNameConverted::class];
}

public function testRequestedFieldsGoThroughTheNameConverter(): void
{
$response = self::createClient()->request(
'GET',
'/sparse_fieldset_name_converted/1?fields[SparseFieldsetNameConverted]=title,name_converted',
['headers' => ['Accept' => 'application/vnd.api+json']],
);

$this->assertResponseIsSuccessful();
$this->assertSame(
['title' => 'Title #1', 'name_converted' => 'Converted #1'],
$response->toArray()['data']['attributes'],
);
}
}
Loading