Skip to content

Commit e786735

Browse files
committed
feat(name): support metadata aware name converter
1 parent 6ba1027 commit e786735

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

src/EventListener/Symfony/AdvancedNameConverterListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(
1818
public function __invoke(PropertyMetadataEvent $event): void
1919
{
2020
if (($event->mapperMetadata->source === 'array' || $event->mapperMetadata->source === \stdClass::class) && $event->source->property === $event->target->property) {
21-
$event->source->property = $this->nameConverter->denormalize($event->target->property, $event->mapperMetadata->target);
21+
$event->source->property = $this->nameConverter->normalize($event->target->property, $event->mapperMetadata->target);
2222
}
2323

2424
if (($event->mapperMetadata->target === 'array' || $event->mapperMetadata->target === \stdClass::class) && $event->source->property === $event->target->property) {

src/Metadata/MetadataFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
5252
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
5353
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
54+
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
5455
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
5556
use Symfony\Component\Uid\AbstractUid;
5657

@@ -343,14 +344,13 @@ public static function create(
343344
$reflectionExtractor = new ReflectionExtractor(accessFlags: $flags);
344345
$phpStanExtractor = new PhpStanExtractor();
345346

346-
if (null !== $nameConverter) {
347-
$eventDispatcher->addListener(PropertyMetadataEvent::class, new AdvancedNameConverterListener($nameConverter));
348-
}
349-
350347
if (null !== $classMetadataFactory) {
348+
$eventDispatcher->addListener(PropertyMetadataEvent::class, new AdvancedNameConverterListener(new MetadataAwareNameConverter($classMetadataFactory, $nameConverter)));
351349
$eventDispatcher->addListener(PropertyMetadataEvent::class, new SerializerMaxDepthListener($classMetadataFactory));
352350
$eventDispatcher->addListener(PropertyMetadataEvent::class, new SerializerGroupListener($classMetadataFactory));
353351
$eventDispatcher->addListener(PropertyMetadataEvent::class, new SerializerIgnoreListener($classMetadataFactory));
352+
} elseif (null !== $nameConverter) {
353+
$eventDispatcher->addListener(PropertyMetadataEvent::class, new AdvancedNameConverterListener($nameConverter));
354354
}
355355

356356
$eventDispatcher->addListener(PropertyMetadataEvent::class, new MapToContextListener($reflectionExtractor));

src/Symfony/Bundle/DependencyInjection/AutoMapperExtension.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,15 @@ public function load(array $configs, ContainerBuilder $container): void
136136
}
137137

138138
if (null !== $config['name_converter']) {
139-
$container
140-
->getDefinition(AdvancedNameConverterListener::class)
141-
->replaceArgument(0, new Reference($config['name_converter']))
142-
->addTag('kernel.event_listener', ['event' => PropertyMetadataEvent::class, 'priority' => -64]);
139+
if ($container->has('automapper.mapping.metadata_aware_name_converter')) {
140+
$container->getDefinition('automapper.mapping.metadata_aware_name_converter')
141+
->setArgument(1, new Reference($config['name_converter']));
142+
} else {
143+
$container
144+
->getDefinition(AdvancedNameConverterListener::class)
145+
->replaceArgument(0, new Reference($config['name_converter']))
146+
->addTag('kernel.event_listener', ['event' => PropertyMetadataEvent::class, 'priority' => -64]);
147+
}
143148
}
144149

145150
$configMappingRegistry = $container->getDefinition('automapper.config_mapping_registry');

src/Symfony/Bundle/Resources/config/event_serializer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
66

77
use AutoMapper\Event\PropertyMetadataEvent;
8+
use AutoMapper\EventListener\Symfony\AdvancedNameConverterListener;
89
use AutoMapper\EventListener\Symfony\SerializerGroupListener;
910
use AutoMapper\EventListener\Symfony\SerializerIgnoreListener;
1011
use AutoMapper\EventListener\Symfony\SerializerMaxDepthListener;
1112
use AutoMapper\Generator\Shared\ClassDiscriminatorResolver;
1213
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
14+
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
1315

1416
return static function (ContainerConfigurator $container) {
1517
$container->services()
@@ -30,5 +32,12 @@
3032

3133
->set('automapper.mapping.class_discriminator_from_class_metadata', ClassDiscriminatorFromClassMetadata::class)
3234
->args([service('serializer.mapping.class_metadata_factory')])
35+
36+
->set('automapper.mapping.metadata_aware_name_converter', MetadataAwareNameConverter::class)
37+
->args([service('serializer.mapping.class_metadata_factory')])
38+
39+
->set(AdvancedNameConverterListener::class)
40+
->args([service('automapper.mapping.metadata_aware_name_converter')])
41+
->tag('kernel.event_listener', ['event' => PropertyMetadataEvent::class, 'priority' => -64])
3342
;
3443
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
AutoMapper\Tests\AutoMapperTest\ConstructorWithSerializedName\Bar {
2+
+isStatic: true
3+
}
4+

tests/AutoMapperTest/ConstructorWithSerializedName/map.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ public function __construct(
1616
}
1717
}
1818

19-
$autoMapper = AutoMapperBuilder::buildAutoMapper();
20-
21-
2219
return (function () {
2320
$autoMapper = AutoMapperBuilder::buildAutoMapper();
2421

0 commit comments

Comments
 (0)