Skip to content

Commit ada0542

Browse files
authored
fix(bundle): allow to set remove default property config (#278)
2 parents b77f09f + 6481df6 commit ada0542

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Fixed
10+
- [GH#278](https://github.com/jolicode/automapper/pull/278) Allow to set remove default property config in symfony bundle
911

1012
## [9.4.0] - 2025-05-30
1113
### Added

docs/bundle/configuration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ automapper:
1515
auto_register: true
1616
map_private_properties: true
1717
allow_readonly_target_to_populate: false
18+
remove_default_properties: true
1819
normalizer:
1920
enabled: false
2021
only_registered_mapping: false
@@ -56,6 +57,10 @@ automapper:
5657
* `map_private_properties` (default: `true`): If the mapper should map private properties;
5758
* `allow_readonly_target_to_populate` (default: `false`): Will throw an exception if you use a readonly class as target
5859
to populate if set to `false`.
60+
* `remove_default_properties` (default: `false`): Before 9.4 when renaming a property, the mapper would keep the old
61+
property name in the target object which resulted in a duplicated property, this option will remove the default properties
62+
from the target object when mapping and will always be true in next major version, you should set it to `true` to avoid
63+
any issues in the future;
5964
* `normalizer`: Configure how the normalizer should behave;
6065
* `enabled` (default: `false`): If the normalizer should be enabled;
6166
* `only_registered_mapping` (default: `false`): If the normalizer should only use the registered mapping;

src/Symfony/Bundle/DependencyInjection/AutoMapperExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use AutoMapper\Loader\EvalLoader;
1515
use AutoMapper\Loader\FileLoader;
1616
use AutoMapper\Loader\FileReloadStrategy;
17+
use AutoMapper\Metadata\MetadataFactory;
1718
use AutoMapper\Normalizer\AutoMapperNormalizer;
1819
use AutoMapper\Provider\ProviderInterface;
1920
use AutoMapper\Symfony\Bundle\CacheWarmup\CacheWarmer;
@@ -73,6 +74,14 @@ public function load(array $configs, ContainerBuilder $container): void
7374
)))
7475
;
7576

77+
if (!$config['remove_default_properties']) {
78+
trigger_deprecation('jolicode/automapper', '9.4', 'Not removing default properties is deprecated and will be always true in future versions, set \'remove_default_properties\' config parameter to true, and check that your mapping is correct.', __CLASS__);
79+
}
80+
81+
$container->getDefinition(MetadataFactory::class)
82+
->setArgument('$removeDefaultProperties', $config['remove_default_properties'])
83+
;
84+
7685
if ($config['map_private_properties']) {
7786
$container->getDefinition('automapper.property_info.reflection_extractor')
7887
->replaceArgument('$accessFlags', ReflectionExtractor::ALLOW_PUBLIC | ReflectionExtractor::ALLOW_PRIVATE | ReflectionExtractor::ALLOW_PROTECTED);

src/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function getConfigTreeBuilder(): TreeBuilder
3232
->booleanNode('auto_register')->defaultTrue()->end()
3333
->booleanNode('map_private_properties')->defaultTrue()->end()
3434
->booleanNode('allow_readonly_target_to_populate')->defaultFalse()->end()
35+
->booleanNode('remove_default_properties')->defaultFalse()->end()
3536
->arrayNode('normalizer')
3637
->children()
3738
->booleanNode('enabled')->defaultFalse()->end()

0 commit comments

Comments
 (0)