Skip to content

Commit 955c4da

Browse files
committed
Make use of the new AdvancedNameConverterInterface
fix #2342 Elasticsearch normalizer is experimental, changed constructor signature
1 parent 359c329 commit 955c4da

27 files changed

+170
-79
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ parameters:
4949
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\MongoDbOdm\\Filter\\(Abstract|Boolean|Date|Exists|Numeric|Order|Range|Search)Filter::isPropertyNested\(\) invoked with 2 parameters, 1 required\.#'
5050
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\MongoDbOdm\\Filter\\(Abstract|Boolean|Date|Exists|Numeric|Order|Range|Search)Filter::splitPropertyParts\(\) invoked with 2 parameters, 1 required\.#'
5151
- '#Method ApiPlatform\\Core\\DataProvider\\CollectionDataProviderInterface::getCollection\(\) invoked with 3 parameters, 1-2 required\.#'
52+
- '#Method Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface::normalize\(\) invoked with 3 parameters, 1 required\.#'
53+
- '#Method Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface::normalize\(\) invoked with 4 parameters, 1 required\.#'

src/Bridge/Elasticsearch/DataProvider/Extension/SortExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ private function getOrder(string $resourceClass, string $property, string $direc
8989
$order = ['order' => strtolower($direction)];
9090

9191
if (null !== $nestedPath = $this->getNestedFieldPath($resourceClass, $property)) {
92-
$nestedPath = null === $this->nameConverter ? $nestedPath : $this->nameConverter->normalize($nestedPath);
92+
$nestedPath = null === $this->nameConverter ? $nestedPath : $this->nameConverter->normalize($nestedPath, $resourceClass);
9393
$order['nested'] = ['path' => $nestedPath];
9494
}
9595

96-
$property = null === $this->nameConverter ? $property : $this->nameConverter->normalize($property);
96+
$property = null === $this->nameConverter ? $property : $this->nameConverter->normalize($property, $resourceClass);
9797

9898
return [$property => $order];
9999
}

src/Bridge/Elasticsearch/DataProvider/Filter/AbstractSearchFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ public function apply(array $clauseBody, string $resourceClass, ?string $operati
7272
continue;
7373
}
7474

75-
$property = null === $this->nameConverter ? $property : $this->nameConverter->normalize($property);
75+
$property = null === $this->nameConverter ? $property : $this->nameConverter->normalize($property, $resourceClass, null, $context);
7676
$nestedPath = $this->getNestedFieldPath($resourceClass, $property);
77-
$nestedPath = null === $nestedPath || null === $this->nameConverter ? $nestedPath : $this->nameConverter->normalize($nestedPath);
77+
$nestedPath = null === $nestedPath || null === $this->nameConverter ? $nestedPath : $this->nameConverter->normalize($nestedPath, $resourceClass, null, $context);
7878

7979
$searches[] = $this->getQuery($property, $values, $nestedPath);
8080
}

src/Bridge/Elasticsearch/DataProvider/Filter/OrderFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ public function apply(array $clauseBody, string $resourceClass, ?string $operati
7070
$order = ['order' => $direction];
7171

7272
if (null !== $nestedPath = $this->getNestedFieldPath($resourceClass, $property)) {
73-
$nestedPath = null === $this->nameConverter ? $nestedPath : $this->nameConverter->normalize($nestedPath);
73+
$nestedPath = null === $this->nameConverter ? $nestedPath : $this->nameConverter->normalize($nestedPath, $resourceClass, null, $context);
7474
$order['nested'] = ['path' => $nestedPath];
7575
}
7676

77-
$property = null === $this->nameConverter ? $property : $this->nameConverter->normalize($property);
77+
$property = null === $this->nameConverter ? $property : $this->nameConverter->normalize($property, $resourceClass, null, $context);
7878
$orders[] = [$property => $order];
7979
}
8080

src/Bridge/Elasticsearch/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function normalize($object, $format = null, array $context = [])
8484
private function populateIdentifier(array $data, string $class): array
8585
{
8686
$identifier = $this->identifierExtractor->getIdentifierFromResourceClass($class);
87-
$identifier = null === $this->nameConverter ? $identifier : $this->nameConverter->normalize($identifier);
87+
$identifier = null === $this->nameConverter ? $identifier : $this->nameConverter->normalize($identifier, $class, self::FORMAT);
8888

8989
if (!isset($data['_source'][$identifier])) {
9090
$data['_source'][$identifier] = $data['_id'];

src/Bridge/Elasticsearch/Serializer/NameConverter/InnerFieldsNameConverter.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Core\Bridge\Elasticsearch\Serializer\NameConverter;
1515

16+
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
1617
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1718
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1819

@@ -23,7 +24,7 @@
2324
*
2425
* @author Baptiste Meyer <[email protected]>
2526
*/
26-
final class InnerFieldsNameConverter implements NameConverterInterface
27+
final class InnerFieldsNameConverter implements AdvancedNameConverterInterface
2728
{
2829
private $decorated;
2930

@@ -35,25 +36,25 @@ public function __construct(?NameConverterInterface $decorated = null)
3536
/**
3637
* {@inheritdoc}
3738
*/
38-
public function normalize($propertyName)
39+
public function normalize($propertyName, string $class = null, string $format = null, $context = [])
3940
{
40-
return $this->convertInnerFields($propertyName, true);
41+
return $this->convertInnerFields($propertyName, true, $class, $format, $context);
4142
}
4243

4344
/**
4445
* {@inheritdoc}
4546
*/
46-
public function denormalize($propertyName)
47+
public function denormalize($propertyName, string $class = null, string $format = null, $context = [])
4748
{
48-
return $this->convertInnerFields($propertyName, false);
49+
return $this->convertInnerFields($propertyName, false, $class, $format, $context);
4950
}
5051

51-
private function convertInnerFields(string $propertyName, bool $normalization): string
52+
private function convertInnerFields(string $propertyName, bool $normalization, string $class = null, string $format = null, $context = []): string
5253
{
5354
$convertedProperties = [];
5455

5556
foreach (explode('.', $propertyName) as $decomposedProperty) {
56-
$convertedProperties[] = $this->decorated->{$normalization ? 'normalize' : 'denormalize'}($decomposedProperty);
57+
$convertedProperties[] = $this->decorated->{$normalization ? 'normalize' : 'denormalize'}($decomposedProperty, $class, $format, $context);
5758
}
5859

5960
return implode('.', $convertedProperties);

src/Bridge/NelmioApiDoc/Parser/ApiPlatformParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private function getPropertyMetadata(ResourceMetadata $resourceMetadata, string
180180
($propertyMetadata->isReadable() && self::OUT_PREFIX === $io) ||
181181
($propertyMetadata->isWritable() && self::IN_PREFIX === $io)
182182
) {
183-
$normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName;
183+
$normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $resourceClass) : $propertyName;
184184
$data[$normalizedPropertyName] = $this->parseProperty($resourceMetadata, $propertyMetadata, $io, null, $visited);
185185
}
186186
}

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ public function prepend(ContainerBuilder $container)
7979
if (!isset($propertyInfoConfig['enabled'])) {
8080
$container->prependExtensionConfig('framework', ['property_info' => ['enabled' => true]]);
8181
}
82-
83-
if (isset($serializerConfig['name_converter'])) {
84-
$container->prependExtensionConfig('api_platform', ['name_converter' => $serializerConfig['name_converter']]);
85-
}
8682
}
8783

8884
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
15+
16+
use ApiPlatform\Core\Exception\RuntimeException;
17+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
20+
/**
21+
* Injects the metadata aware name converter if available.
22+
*
23+
* @internal
24+
*
25+
* @author Antoine Bluchet <[email protected]>
26+
*/
27+
final class MetadataAwareNameConverterPass implements CompilerPassInterface
28+
{
29+
/**
30+
* {@inheritdoc}
31+
*
32+
* @throws RuntimeException
33+
*/
34+
public function process(ContainerBuilder $container)
35+
{
36+
if (!$container->hasAlias('api_platform.name_converter') && $container->hasDefinition('serializer.name_converter.metadata_aware')) {
37+
$container->setAlias('api_platform.name_converter', 'serializer.name_converter.metadata_aware');
38+
}
39+
}
40+
}

src/Hal/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private function populateRelation(array $data, $object, string $format = null, a
181181

182182
$relationName = $relation['name'];
183183
if ($this->nameConverter) {
184-
$relationName = $this->nameConverter->normalize($relationName);
184+
$relationName = $this->nameConverter->normalize($relationName, $class, $format, $context);
185185
}
186186

187187
if ('one' === $relation['cardinality']) {

0 commit comments

Comments
 (0)