-
-
Notifications
You must be signed in to change notification settings - Fork 916
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
#3539 Support BeanMapping#ignoredTargets instead of repeating @Mapping(target = "foo", ignore = true) #3545
base: main
Are you sure you want to change the base?
Conversation
…@mapping(target = "foo", ignore = true)
Thanks for your PR @chenzijia12300. Can we perhaps add a test case when an unknown property is used in |
Hi @filiphr, thanks for your review, I have added a test case |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checked it out and had a look :)
@ProcessorTest | ||
@WithClasses({Source.class, Target.class, BeanMappingIgnoreTargetMapper.class}) | ||
public void shouldIgnoreTargetProperty() { | ||
BeanMappingIgnoreTargetMapper.INSTANCE.convert( new Source() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't there be an assertion? :)
public void shouldIgnoreTargetProperty() {
Source source = new Source();
Source.NestedSource nested = new Source.NestedSource();
source.setAge( 18 );
source.setName( "John" );
source.setNested( nested );
nested.setScore( 50.0 );
Target convert = BeanMappingIgnoreTargetMapper.INSTANCE.convert( source );
assertThat(convert).hasNoNullFieldsOrPropertiesExcept( "address" );
assertThat(convert.getNested()).hasNoNullFieldsOrPropertiesExcept( "flag" );
}
SHouldn't we add a test like that? :) Or is it enough thats it does not generate an error that we have unmapped targets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree about this one. We should add an assertion as well.
@@ -284,6 +285,20 @@ else if ( !method.isUpdateMethod() ) { | |||
|
|||
initializeMappingReferencesIfNeeded( resultTypeToMap ); | |||
|
|||
if ( beanMapping != null ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a new method?
Mappers.getMapper( BeanMappingIgnoreTargetMapper.class ); | ||
|
||
@BeanMapping( ignoreTargets = {"nested.flag", "address"} ) | ||
Target convert( Source source ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mapping( target = "address", ignore = true)
@BeanMapping( ignoreTargets = {"nested.flag", "address"} )
Target convert( Source source );
Should this lead to warning? What happens if we do that.
BeanMappingIgnoreTargetMapper INSTANCE = | ||
Mappers.getMapper( BeanMappingIgnoreTargetMapper.class ); | ||
|
||
@BeanMapping( ignoreTargets = {"nested.flag", "address"} ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this also lead to: Target property "address" must not be mapped more than once.
@BeanMapping( ignoreTargets = {"nested.flag", "address", "address"} )
this leads to: Target property "address" must not be mapped more than once.
@Mapping( target = "address", ignore = true)
@Mapping( target = "address", ignore = true)
does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the best solution for this would be to do it in the MapperCreationProcessor
similar to how we are doing it for @BeanMapping( ignoreByDefault = true )
.
Additionally, as @zyberzebra mentioned in some other comments. We need to make sure that people do not use duplicates and that you can't define things in @BeanMapping#ignoreTargets
and @Mapping
in the same time.
diagnostics = { | ||
@Diagnostic(type = ErroneousBeanMappingIgnoreTargetMapper.class, | ||
kind = javax.tools.Diagnostic.Kind.ERROR, | ||
line = 19, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reported line should be on the @BeanMapping
not on the method.
@ProcessorTest | ||
@WithClasses({Source.class, Target.class, BeanMappingIgnoreTargetMapper.class}) | ||
public void shouldIgnoreTargetProperty() { | ||
BeanMappingIgnoreTargetMapper.INSTANCE.convert( new Source() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree about this one. We should add an assertion as well.
@zyberzebra @filiphr Thank you very much for your review. I will try to modify these problems. |
This commit attempts to implement #2599, mapping
BeanMapping#ignoredTargets
to multipleMapping#ignore
annotations.If there are any necessary changes, I'll do my best to address them.