Skip to content

fix(bundle): allow to set remove default property config #278

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

Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

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

## [9.4.0] - 2025-05-30
### Added
Expand Down
5 changes: 5 additions & 0 deletions docs/bundle/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ automapper:
auto_register: true
map_private_properties: true
allow_readonly_target_to_populate: false
remove_default_properties: true
normalizer:
enabled: false
only_registered_mapping: false
Expand Down Expand Up @@ -56,6 +57,10 @@ automapper:
* `map_private_properties` (default: `true`): If the mapper should map private properties;
* `allow_readonly_target_to_populate` (default: `false`): Will throw an exception if you use a readonly class as target
to populate if set to `false`.
* `remove_default_properties` (default: `false`): Before 9.4 when renaming a property, the mapper would keep the old
property name in the target object which resulted in a duplicated property, this option will remove the default properties
from the target object when mapping and will always be true in next major version, you should set it to `true` to avoid
any issues in the future;
* `normalizer`: Configure how the normalizer should behave;
* `enabled` (default: `false`): If the normalizer should be enabled;
* `only_registered_mapping` (default: `false`): If the normalizer should only use the registered mapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use AutoMapper\Loader\EvalLoader;
use AutoMapper\Loader\FileLoader;
use AutoMapper\Loader\FileReloadStrategy;
use AutoMapper\Metadata\MetadataFactory;
use AutoMapper\Normalizer\AutoMapperNormalizer;
use AutoMapper\Provider\ProviderInterface;
use AutoMapper\Symfony\Bundle\CacheWarmup\CacheWarmer;
Expand Down Expand Up @@ -73,6 +74,14 @@ public function load(array $configs, ContainerBuilder $container): void
)))
;

if (!$config['remove_default_properties']) {
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__);
}

$container->getDefinition(MetadataFactory::class)
->setArgument('$removeDefaultProperties', $config['remove_default_properties'])
;

if ($config['map_private_properties']) {
$container->getDefinition('automapper.property_info.reflection_extractor')
->replaceArgument('$accessFlags', ReflectionExtractor::ALLOW_PUBLIC | ReflectionExtractor::ALLOW_PRIVATE | ReflectionExtractor::ALLOW_PROTECTED);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('auto_register')->defaultTrue()->end()
->booleanNode('map_private_properties')->defaultTrue()->end()
->booleanNode('allow_readonly_target_to_populate')->defaultFalse()->end()
->booleanNode('remove_default_properties')->defaultFalse()->end()
->arrayNode('normalizer')
->children()
->booleanNode('enabled')->defaultFalse()->end()
Expand Down